简体   繁体   中英

Jquery comparing elements

I have an array on javascript and i insert the elements on it like this:

 var parentRow = $(button).parent().parent();
 list.push({ parent: parentRow, detailRow: newRow });

On the click of another button i do the following:

      var parentRow = $(button).parent().parent();
      var detailRow = null;

      for (var i in list) {
        if ($(list[i].parent) == $(parentRow)) {
          detailRow = list[i].detailRow;
        }
      }

The point is: The if comparing to two elements should return TRUE, because they are the same DOM element....the same i added before, but it return FALSE.

I would like to know how i compare this two elements to get TRUE there.

Try:

if (parentRow.has(list[i])) {

They are not the same objects, because they don't refer to the same jQuery instance.

Simple solution: Don't use jQuery and do it with normal DOM methods.

jQuery solution: Use .is()

You need to compare the native elements, not the jQuery-wrapped elements. jQuery's DOM methods returns not the elements themselves but a jQuery object.

if (list[i].parent[0] === parentRow[0]) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM