簡體   English   中英

如何從動態生成的iframe中刪除onload事件?

[英]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);">&lt;a 
href="https://www.xxxxxxx.co.uk/s/YEXFX/"&gt;Please take our 
survey&lt;/a&gt;</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);">&lt;a href="https://www.xxxxxxx.co.uk/s/YEXFX/"&gt;Please take our survey&lt;/a&gt;</iframe>`; onload = function() { document.body.innerHTML += iframe }; </script> </head> <body> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM