【发布时间】:2019-01-21 09:51:57
【问题描述】:
我正在尝试编写代码以在点击任何网站的广告后隐藏和显示<div>。此广告是脚本的结果。那么如何在<script>标签中使用onclick事件呢?
我的代码:
<script>
function myFunction() {
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
<div class="bnrdiv" onClick="myFunction()">
<script type="text/javascript">
var ad_idzone = "3265310",
ad_width = "468",
ad_height = "60";
</script>
<script type="text/javascript" src="https://ads.exdynsrv.com/ads.js"></script>
<noscript><iframe src="https://syndication.exdynsrv.com/ads-iframe-display.php?idzone=3265310&output=noscript&type=468x60" width="468" height="60" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></noscript>
</div>
<div id="myDIV">
This is my DIV element.
</div>
【问题讨论】:
-
您的代码有什么问题?
bnrdiv不是里面有广告的DIV吗? -
广告可能有自己的点击处理程序将您链接到他们正在宣传的网站,它可能使用
event.stopPropagation(),因此点击事件永远不会冒泡到您的 DIV。
标签: javascript jquery html onclick dom-events