JQuery在创建DOM元素后执行某些操作 - DOMNodeInserted无法正常工作

时间:2016-02-10 11:28:09

标签: javascript jquery html

好的,我有一个空div,稍后会动态填充一些内容:

<div class="row no-margin">
    <div id="sensorInfoContent">
    </div>
</div>

后来它充满了其他一些div和内容。我需要选择这个子div:

<div class="circDash" id="batPerc" accesskey="30" style="height:100px; background-color:#000000;">

并在其上调用此函数:

$(*DIV*).circleProgress({
            value: 0.75,
            size: 80,
            fill: {
                gradient: ["red", "orange"]
           }
        });

我尝试点击它,它正在工作。像这样:

$('#sensorInfoContent').on("click", ".circDash", function() {

    $(this).circleProgress({
        value: 0.75,
        size: 80,
        fill: {
            gradient: ["red", "orange"]
       }
    });
});

但我需要在动态添加元素后才能完成,而不是点击。试过DOMNodeInserted,它不起作用:

    $('#sensorInfoContent').on("DOMNodeInserted", ".circDash", function() {

        $(this).circleProgress({
            value:

 0.75,
        size: 80,
        fill: {
            gradient: ["red", "orange"]
       }
    });
}); 

任何想法,替代方案或解决方法?

2 个答案:

答案 0 :(得分:1)

只需手动触发点击

$('#sensorInfoContent').on("click", ".circDash", function() {

    $(this).circleProgress({
        value: 0.75,
        size: 80,
        fill: {
            gradient: ["red", "orange"]
       }
    });
});

喜欢这个

$('.circDash').trigger('click');

答案 1 :(得分:0)

好吧,我通过盲目猜测自己解决了这个问题。

脚本代码最初写在页面顶部,现在我把脚本放在这个.circDash div下面,它正在工作! :d

这只是脚本定位,我想现在脚本在创建div后运行。

我是JQuery / JS的新手所以...这可能是一个新手的错误。 :D干杯!

编辑:如果有人想要并且知道回答为什么函数on.("click",...起作用而函数on.("DOMNodeInserted",...没有,那么无论我的解决方案是什么,这都是最好的答案。

相关问题