【发布时间】:2013-09-29 13:38:59
【问题描述】:
让我来说明我的问题:我有一个外部 JavaScript 库,它可以根据用户输入和交互动态地为我创建某些 HTML 元素,我正在寻找一个脚本来自动将某个类添加到这些动态创建的元素。假设我也无法编辑我正在使用的外部 JavaScript 库。
这很可能吗?如果是这样,怎么做?如果不是,这可能是糟糕的实现设计的副作用吗?
我曾想过以某种方式监控 DOM 以查看它何时更新,然后将类添加到这些新元素中,但这似乎很麻烦并且可能没有必要。
提前感谢您的任何想法/解决方案!
编辑:
根据要求,这是我试图通过代码示例完成的简化示例:
// function in external library, assume it cannot be edited!
function addElement() {
$('body').append($('<div class="newly_created_element"></div>'));
}
// my code, looking to add the class name to the newly-created elements
// from the external function above...
// pseudo-coded as I have no idea how to do this!
$(function(){
when (new element added) {
add class "my_own_class";
}
});
我希望这是有道理的!
【问题讨论】:
-
发布代码示例/有关当前工作原理的更多详细信息?
-
如果第三方库不提供任何自定义事件,那就很难了
-
一个好的 API 允许您定义一个回调函数,例如在每个元素创建后调用该函数。
-
您可以在mutation events 获得战利品,但 IE 不支持 IE
-
也许这行得通。 $("element-root").bind(DOMSubtreeModified,"CustomHandler");在这里找到stackoverflow.com/questions/9488653/…
标签: javascript jquery html css dom