DOM operation to get the same element in different positions, will they be equal?

DOM operation to get the same element in different positions, will they be equal?

The answer is no, test it:

<html5>
    <head>
        <meta charset="utf-8">
        <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
        <title>Jquery与DOM测试</title>
    </head>
    <body>
        <!-- 两个处于不同位置,但是内容相等的span标签  -->
        <div id="div1"><span onclick="test(this)">这个span</span></div>
        <br>
        <br>
        <div id="div2"><span onclick="test(this)">这个span</span></div>
    </body>
    <script>
            //点击div1中的span通过this传过来dom对象d_obj
        function test(d_obj){
            //获取div2的span(jquery转dom与dom方式直接获取相同)
            console.log($("#div2 span")[0]);
            //比较两者
            if($("#div2 span")[0] == d_obj){
                console.log("即使所处位置不一样,但是标签内容相同就相等");
            }else{
                console.log("不一样的位置即使标签相同也是不同的");
            }
        }
    </script>
    </html>

image-20200421002712422

There may be values ​​in the object to distinguish the elements in different positions, so even if the two tags are exactly the same, even if the content to the attributes are the same, the objects after obtaining the elements are not equal.

Guess you like

Origin www.cnblogs.com/taoxiaoyao/p/12741761.html