最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin4PV0评论

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() )
Post a comment

comment list (0)

  1. No comments so far