Convert jQuery element to a regular dom element

前端 未结 3 1150
暖寄归人
暖寄归人 2020-12-30 02:51

A regular dom element is such as:

document.getElementById(\'a\')

or

document.createElement(\'div\')

But jQuery retu

相关标签:
3条回答
  • 2020-12-30 03:06

    You can reference the DOM element with .get(0) or [0], eg $('#foo')[0] assuming there is just one.

    0 讨论(0)
  • 2020-12-30 03:14
    $('#a')[0]
    

    // oops my answer is too short

    0 讨论(0)
  • 2020-12-30 03:21

    That is what get() method was made for by JQuery team, consider:

    var elem = $('#a')[0];
    

    Or

    var elem1 = $('#a').get(0);
    var elem2 = document.getElementById('a');
    alert(elem1 === elem2);
    

    or longer version:

    <!DOCTYPE HTML>
    <html>
    <body>   
    <a id='a'></a>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
      <script>
    
        var elem1 = $('#a').get(0);
        var elem2 = document.getElementById('a');
        alert(elem1 === elem2);
    
      </script>  
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题