3

So, I have a page with messages. I'm using jQuery.load (requesting the exact same page) to refresh the page smoothly once every X seconds.

The problem is that if there are images on the page, they get reloaded.

So basically what I want to do, is to still use the load method, but to only update the changed elements.

Is it possible to compare jQuery('.message-wrapper').first() with jQuery('.message-wrapper').last() and if they have the exact same structure / content, it should return true. Right now if you compare 2 HTML nodes (so JS, not jQuery), you get true only if they are one and the same element.

What I want to do is check the content to see if it's different.

Disclaimer: I've seen a few similar questions, but none have a working solution.

4

1 回答 1

4

要比较您可以使用的元素的文本内容text(),或比较标记,请使用html()

$('.message-wrapper').first().text() == $('.message-wrapper').last().text()

要删除字符串前后的空格,有时这可能是个问题,您可以将其包装在 jQuery 的$.trim方法中:

$.( $('.message-wrapper').first().html() ) === $.( $('.message-wrapper').last().html() )
于 2013-05-18T02:22:55.197 回答