jQuery(event): watch element style(jQuery(event):监视元素样式)
问题描述
假设有这样的元素
<div class="watch-me" style="display: none;">Watch Me Please</div>
我们可以看到上面的元素样式是display: none
,我如何制作脚本来观看这个元素.当该元素样式更改为 display: block
时会触发一些代码.
as we can see the element above style is display: none
, how can i make script to watch this element. when that element style is change to display: block
then there is some code will be trigger.
非常感谢...
推荐答案
据我所知,唯一的方法就是使用计时器.
As far as I know, the only way to do this would be with a timer.
我创建了一个小型 jQuery 插件(是的,就在这里,现在),它针对 jQuery 集执行此操作:
I created a small jQuery plugin (yes, right here, right now) that does this against a jQuery set:
EDIT 这个插件现在在 GitHub 上:https://github.com/jacobrelkin/jquery-watcher
EDIT this plugin is now on GitHub: https://github.com/jacobrelkin/jquery-watcher
$.fn.watch = function(property, callback) {
return $(this).each(function() {
var self = this;
var old_property_val = this[property];
var timer;
function watch() {
if($(self).data(property + '-watch-abort') == true) {
timer = clearInterval(timer);
$(self).data(property + '-watch-abort', null);
return;
}
if(self[property] != old_property_val) {
old_property_val = self[property];
callback.call(self);
}
}
timer = setInterval(watch, 700);
});
};
$.fn.unwatch = function(property) {
return $(this).each(function() {
$(this).data(property + '-watch-abort', true);
});
};
用法:
$('.watch-me').watch('style', function() {
//"this" in this scope will reference the object on which the property changed
if($(this).css('display') == 'block') {
//do something...
}
});
要清除此属性观察器:
$('.watch-me').unwatch('style');
这篇关于jQuery(event):监视元素样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jQuery(event):监视元素样式


- 400或500级别的HTTP响应 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06