How to get nodeType using jquery

前端 未结 5 1396
借酒劲吻你
借酒劲吻你 2021-01-13 02:52

I want to get nodeType and then compare it to where it is text node or element node.





        
相关标签:
5条回答
  • 2021-01-13 03:33

    As noted above, $('.jj').get(0).nodeType works.

    Same as $('.jj').first().nodeType

    or $('.jj').prop('nodeType')

    .prop() : Get the value of a property for the first element in the set of matched elements or set one or more properties for every matched element.

    0 讨论(0)
  • 2021-01-13 03:36

    To get the DOM Node you can use [0]:

    var mm = $(".jj")[0];
    if (mm.nodeType === 1) {
        // Node.ELEMENT_NODE
    }
    

    However, <div> element will never be a text node and won't have a nodeValue.

    Text node is the first child of your <div> element. So the following code will give you "value":

    alert(mm[0].firstChild.nodeValue);
    
    0 讨论(0)
  • 2021-01-13 03:44

    I know it wasn't the question, but to see what type it is (DIV, SPAN, etc), use tagName.

    var mm = $('.jj').get(0);
    alert(mm.tagName);
    
    0 讨论(0)
  • 2021-01-13 03:50
    <script type="text/javascript">
    $(function(){
        var mm= $('.jj')
        alert(mm.get(0).nodeValue)
        })
    </script>
    

    or

    <script type="text/javascript">
    $(function(){
        var mm= $('.jj')
        alert(mm[0])
        })
    </script>
    

    Because a jquery collection is a "wrapped set" of DOM elements.

    0 讨论(0)
  • 2021-01-13 03:58

    try to access to those properties with

    var mm = $('.jj').get(0);
    alert(mm.nodeValue);
    alert(mm.nodeType)
    
    0 讨论(0)
提交回复
热议问题