Last active
December 20, 2015 17:48
-
-
Save 6174/6171106 to your computer and use it in GitHub Desktop.
dom change event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://stackoverflow.com/questions/1449666/create-a-jquery-special-event-for-content-changed | |
(function(){ | |
var interval; | |
jQuery.event.special.contentchange = { | |
setup: function(){ | |
var self = this, | |
$this = $(this), | |
$originalContent = $this.text(); | |
interval = setInterval(function(){ | |
if($originalContent != $this.text()) { | |
$originalContent = $this.text(); | |
jQuery.event.handle.call(self, {type:'contentchange'}); | |
} | |
},100); | |
}, | |
teardown: function(){ | |
clearInterval(interval); | |
} | |
}; | |
})(); | |
jQuery.fn.watch = function( id, fn ) { | |
return this.each(function(){ | |
var self = this; | |
var oldVal = self[id]; | |
$(self).data( | |
'watch_timer', | |
setInterval(function(){ | |
if (self[id] !== oldVal) { | |
fn.call(self, id, oldVal, self[id]); | |
oldVal = self[id]; | |
} | |
}, 100) | |
); | |
}); | |
return self; | |
}; | |
jQuery.fn.unwatch = function( id ) { | |
return this.each(function(){ | |
clearInterval( $(this).data('watch_timer') ); | |
}); | |
}; | |
jQuery.fn.valuechange = function(fn) { | |
return this.bind('valuechange', fn); | |
}; | |
jQuery.event.special.valuechange = { | |
setup: function() { | |
jQuery(this).watch('value', function(){ | |
jQuery.event.handle.call(this, {type:'valuechange'}); | |
}); | |
}, | |
teardown: function() { | |
jQuery(this).unwatch('value'); | |
} | |
}; | |
var title = $("b.facility"); | |
var title = $('#title'); | |
//method two | |
//the element I want to monitor | |
title.bind('DOMNodeInserted', function(e) { | |
alert('element now contains: ' + $(e.target).html()); | |
}); | |
//method three | |
//https://developer.mozilla.org/zh-CN/docs/DOM/MutationObserver | |
var BrowserMutationObserver = window.MutationObserver || window.WebKitMutationObserver; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
为了兼容ie6, 看来只能setInterval了!