最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Compare 2 elements in jQuery (by content) - Stack Overflow

programmeradmin1浏览0评论

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 pare 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 pare 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.

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 pare 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 pare 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.

Share Improve this question edited May 18, 2013 at 0:06 Eduard Luca asked May 17, 2013 at 23:38 Eduard LucaEduard Luca 6,61218 gold badges88 silver badges142 bronze badges 6
  • 3 $('.message-wrapper').first().text() == $('.message-wrapper').last().text() – adeneo Commented May 17, 2013 at 23:46
  • @adeneo if the have the exact same structure / content, it should return true – sabithpocker Commented May 17, 2013 at 23:49
  • @adeneo if you'd be so kind to post that as an answer, I'd be glad to accept it. Now I'm ashamed I even asked this question. .text()... wow. – Eduard Luca Commented May 18, 2013 at 0:04
  • @sabithpocker yes, I requested that, and adeneo's solution wouldn't cover it, but I can deal with that separately, as most of my cases have the same structure, it's just a few exceptions that don't. – Eduard Luca Commented May 18, 2013 at 0:05
  • 1 You can use .html() instead of .text() to account for both structure and content. – bfavaretto Commented May 18, 2013 at 0:28
 |  Show 1 more ment

1 Answer 1

Reset to default 4

To pare the text content of your elements you can use text(), or to pare the markup, use html() :

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

To remove spaces before and after the string, which can be an issue sometimes, you can wrap it in jQuery's $.trim method :

$.( $('.message-wrapper').first().html() ) === $.( $('.message-wrapper').last().html() )
发布评论

评论列表(0)

  1. 暂无评论