首先,没有开箱即用的css-changes事件,但是您可以自己创建一个,仅
onchange用于
:input元素。不适用于CSS更改。
有两种跟踪CSS更改的方法。
- 每x次(示例中为500毫秒)检查DOM元素的css变化。
- 当触发事件 您 更改元素的CSS。
- 使用
DOMAttrModified
突变事件。但是已弃用,因此我将跳过它。
第一种方式:
var $element = $("#elementId");var lastHeight = $("#elementId").css('height');function checkForChanges(){ if ($element.css('height') != lastHeight) { alert('xxx'); lastHeight = $element.css('height'); } setTimeout(checkForChanges, 500);}
第二种方式:
$('#mainContent').bind('heightChange', function(){ alert('xxx'); });$("#btnSample1").click(function() { $("#mainContent").css('height', '400px'); $("#mainContent").trigger('heightChange'); //<==== ...});
如果您控制css的更改,第二个选择是更优雅,更有效的方法。
资料:
- 绑定:
Description: Attach a handler to an event for the elements.
- 触发:
Description: Execute all handlers and behaviors attached to the matched elements for the given event type.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)