html element content change event
i need to known event of content change my div element, how can i do it with jquery?
<div id="mydiv">text before</div>
after some time
<div id="mydiv>other content</div>
i do such way
$开发者_如何学JAVA("#mydiv").change(function() { alert('yes'); });
by doesn't work
(function(){
var interval;
jQuery.event.special.contentchange = {
setup: function(data, namespaces) {
var $this = $(this);
var $originalContent = $this.text();
interval = setInterval(function(){
if($originalContent != $this.text()) {
console.log('content changed');
$originalContent = $this.text();
jQuery.event.special.contentchange.handler();
}
},500);
},
teardown: function(namespaces){
clearInterval(interval);
},
handler: function(namespaces) {
jQuery.event.handle.apply(this, arguments)
}
};
})();
$("#mydiv").bind("contentchange", function() {
alert('yes'); ///no alert! why?
});
but i see logs in console!
You could trigger a custom event after you change the content of the element:
var $mydiv = $("#mydiv");
// set up the custom event handler
$mydiv.bind("contentchange", function() {
alert("changed");
});
// change the content and trigger a custom event
$mydiv.html("other content").trigger("contentchange");
// and again
$mydiv.html("yet other content").trigger("contentchange");
"The change event is sent to an element when its value changes.
This event is limited to <input> elements, <textarea> boxes and <select> elements. "
You cannot use the change event on divs.
精彩评论