Test if two elements are the same

前端 未结 7 1481
予麋鹿
予麋鹿 2020-12-07 21:32

I would suspect this to work at first:

if ($(\'#element\') == $(\'#element\')) alert(\'hello\');

But it does not. How does one test if ele

7条回答
  •  广开言路
    2020-12-07 22:17

    Like silky or Santi said, a unique ID or class would be the easiest way to test. The reason your if statements don't work like you'd expect is because it's comparing 2 objects and seeing if they're the same object in memory.

    Since it's always a new object getting created by $(this), they can never equal each other. That's why you have to test on a property of the object. You could get away with no unique id/class if each openActivity element was guaranteed to have different content that you could test against.

提交回复
热议问题