Monitor Element Content Modifications with jQuery
The change() function effectively detects alterations to form elements. However, can you pinpoint changes made to the content of DOM elements?
Consider this scenario:
<div>
The following code will not suffice:
$("#content").change(function() { // Do something });
This code only responds to changes in form elements. To monitor content changes, explore these alternative approaches:
Approach 1: jQuery Chaining and Traversing
$("#content").html('something').end().find(whatever)....
Approach 2: Custom Events with triggerHandler()
$("#content").html('something').triggerHandler('customAction'); $('#content').unbind().bind('customAction', function(event, data) { // Custom-action });
Note: While the original post focused on content changes in divs, this methodology can also detect modifications made through html() or append().
For more details on the triggerHandler() function, refer to the jQuery documentation: http://api.jquery.com/triggerHandler/
The above is the detailed content of How Can I Detect Content Changes in DOM Elements Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!