[英]onload event from iframe that is dynamically generated via ajax is not firing in IE
[英]How to remove onload event from the dynamically generated iframe?
<div class="voc_container content-container">
<div class="voc_content">
<h3 class="heading-small">Tell us what you think of this page</h3>
<span class="voc_content_open">Take a short survey to give us your feedback</span>
<span class="voc_content_close js-hidden">Close</span>
<div class="voc_content_survey js-hidden">
<script id="ss-embed-380173">
(function (d, w) {
var s, ss;
ss = d.createElement('script');
ss.type = 'text/javascript';
ss.async = true;
ss.src = ('https:' == d.location.protocol ? 'https://' : 'http://') +
'www.xxxxxx.co.uk/s/r/embed.aspx?i=341674&c=380173';
};
s = d.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ss,
s); })(document, window);</script></div>
</div>
</div>**strong text**
生成html作为
<iframe id="ss-embed-frame-380173"
src="https://www.xxxxxxx.co.uk/s/YEXFX/" frameborder="0" style="border:
0px currentColor; border-image: none; width: 100%; height: 400px; padding-
bottom: 4px;" onload="window.scrollTo(0, document.getElementById('ss-embed-
frame-380173').offsetTop);"><a
href="https://www.xxxxxxx.co.uk/s/YEXFX/">Please take our
survey</a></iframe>
我尝试使用createelement删除列表器
ss.removeEventListener("onload","window.scrollTo(0,
document.getElementById('ss-embed-frame-380173').offsetTop);");
ss.onload=function(){}
从动态生成的事件中删除onload事件,但不会将其删除。
谁能帮助我指导如何从动态生成的iframe元素中删除onload事件?
onload="window.scrollTo(0, document.getElementById('ss-embed-
frame-380173').offsetTop);
您可以使用jQuery。
<script type="text/javascript">
$("#ss-embed-frame-380173").removeAttr("onload");
</script>
您可以使用MutationObserver
观察元素追加到document
上,如果id
或其他选择器匹配,则对元素调用.removeAttribute("onload")
,观察者实例使用.disconnect()
<!DOCTYPE html> <html> <head> <script> const config = { childList: true, subtree: true }; const observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { mutation.addedNodes.forEach(function(node) { if (new RegExp("^ss-embed-frame").test(node.id)) { node.removeAttribute("onload"); observer.disconnect(); console.log(node.getAttribute("onload"), node.outerHTML); } }) }) }) observer.observe(document.documentElement, config); </script> <script> let iframe = `<iframe id="ss-embed-frame-380173" src="data:text/plain,abc" frameborder="0" style="border: 0px currentColor; border-image: none; width: 100%; height: 400px; padding- bottom: 4px;" onload="window.scrollTo(0, document.getElementById('ss-embed- frame-380173').offsetTop);"><a href="https://www.xxxxxxx.co.uk/s/YEXFX/">Please take our survey</a></iframe>`; onload = function() { document.body.innerHTML += iframe }; </script> </head> <body> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.