我想对 DOM 上的特定元素调用函数,例如:
$(".red").css({backgroundColor: "pink"});
它适用于 DOM 中已经存在的任何元素,但我也希望在动态添加到 DOM 的元素中调用此方法。
我尝试过类似的事情:
$(".red").on(function(){ this.css({backgroundColor: "pink"}) });
or:
$(".red").on("load", function(){ this.css({backgroundColor: "pink"}) });
但没有任何成功。
检查jsFiddle http://jsfiddle.net/fguillen/22kwt/
Update
The .css()
call 只是一个例子,我实际上想调用其他类型的函数
你很接近。尝试:
$(document).on("load", ".red", function(){ this.css({backgroundColor: "pink"}) });
哎呀,这不行。这确实http://jsfiddle.net/4Bv9r/ http://jsfiddle.net/4Bv9r/
$('body').on('DOMNodeInserted', ".red", function(){ $(this).css({backgroundColor: "pink"}) });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)