How to compare two HTML elements

前端 未结 4 1504
梦毁少年i
梦毁少年i 2020-12-01 06:19

How can we compare two HTML elements whether they are identical or not ?

I tried this thing but no luck

Hi this is sachin ten
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 07:09

    You can use:

    element1.isEqualNode(element2);
    

    In your specific example:

    var divs = $(".a");
    if ( divs.get(0).isEqualNode(divs.get(1)) ) alert("Same");
    

    The DOM Level 3 Core Spec has all the details. Essentially this returns true of the two nodes have matching attributes, descendents, and the descendents' attributes.

    There's a similar .isSameNode() that returns true only if both elements are the same node. In your example, these are not the same nodes, but they are equal nodes.

提交回复
热议问题