【问题标题】:Jquery DOMNodeInserted not working in Chrome but works in IEJquery DOMNodeInserted 在 Chrome 中不工作但在 IE 中工作
【发布时间】:2018-02-14 11:33:20
【问题描述】:

我有一个要开发的模块,其中我有一个父表,并且对于每一行都有一个子表,它们都使用 JSON 数组通过 Ajax 调用使用 jQuery 动态生成。

现在,每次将具有“childtable”类的子表插入显示子表 ID 的父表行时,我都需要触发警报。

我尝试使用 jQuery DOMNodeInserted 事件来实现这一点,它在 IE 中非常适合我,但在 Chrome 中不起作用。

我使用的是 Google Chrome 版本 56.0.2924.87。这段代码如下所示。

$('body').on('DOMNodeInserted', 'table.childtable', function(e) {
if ( ! $.fn.DataTable.isDataTable('#'+$(this).attr('id'))) {

var tab='#'+$(this).attr('id');

 alert(tab);  //fire id as alert

 $('#'+$(this).attr('id')).DataTable({
        "bSort" : false,
        "scrollCollapse": true,
        "searching": false,
        "bInfo" : false,
        "paging": false,
        "columnDefs": [{
               "targets": [ 0 ],
               "visible": false
            }]

 });
  $( tab).find( "thead tr th" ).each( function( index1 ) {
    index1 += 1;
  $(tab).find("tbody tr td:nth-child(" + index1 + ")").attr("data-title", $(this).text());
});


}


});

【问题讨论】:

  • DOMNodeInserted 是已废弃的Mutation Events 的一部分。请改用Mutation Observers
  • 我试图阅读有关突变观察者的信息。但我无法实现它..任何人都可以帮助我编写代码。

标签: javascript jquery html dom cross-browser


【解决方案1】:

经过长时间的搜索,我终于找到了我的问题的答案。感谢 Uzair Farooq。
上面的事情可以使用在 Mutation 观察者上工作的到达.js 来实现。
点击这里下载该文件。 arrive.js


代码:

$('body').arrive('table.childtable', function(){
   alert('#'+$(this).attr('id'));
});


现在我担心的是我无法在旧浏览器中使用此代码。旧浏览器不支持 Bcoz 突变观察者。

那么我们如何确定浏览器是否支持已弃用的突变事件?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2019-12-06
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    相关资源
    最近更新 更多