繁体   English   中英

从外部脚本生成的元素中删除DOM元素

[英]Removing a DOM element from an element which is generated by an external script

在页面中,我有由外部脚本生成的第三方广告内容。 加载此内容需要一些时间。 我想从广告内容中删除换行符 - 为此,必须等到外部脚本加载并且该脚本中调用的所有函数都已停止运行。 我正在修改以下想法,但我想它只会等到外部脚本加载:

$.getScript( "http://www.external_site.com/widget.js").done( function(data, textStatus, jqxhr) {
     $('.result').find('br').remove();  
});

在调用.remove()函数之前,等待外部脚本执行所有DOM操作的最佳方法是什么?

一种方法似乎是监听从新创建和插入的<script>元素触发的onreadystatechangeload事件,其中运行JavaScript。

这适用于链接(简单)演示,但我不能证明它对大型项目的可靠性:

// listening for the onreadystatechange and/or load
// events fired from the newly-added "script" elements:
$('script').on('onreadystatechange load', function (e) {
    // performs the following simple/contrived functionality:
    $('body').find('br').remove();
});

JS小提琴演示

参考文献:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM