我有一个函数,每次点击页面时都应该调用该函数。
所以我尝试将 EventListener
添加到文档中:
document.onclick = function() { /* Do Something */ };
但问题是我的网站上有很多框架
(I know frames are evil and not supported in HTML 5)
所以我尝试向每个帧添加一个 EventListener
:
window.onload = window.setInterval(
function() {
frames = getFrames();
for(var i=0;i<frames.length;i++)
frames[i].addEventListener("click", function () {
//Do Something
}
});
}, 50);
function getFrames() {
var frameset = document.getElementsByTagName("FRAMESET")[1];
var frame = frameset.getElementsByTagName("FRAME");
return frame;
}
我的 HTML 看起来像这样:
<frameset rows="100px,30px,*,5%">
<frame src="topbar.php">
<frame src="toolbar.php">
<frameset cols="25%,40%,*%">
<frame src="content.html>
<frame src="content.html">
<frame src="content.html">
</frameset>
<frame src="endbar.php">
</frameset>
现在正在合作:
window.onload = window.setInterval(
function() {
frames = getFrames();
for(var i=0;i<frames.length;i++)
frames[i].contentDocument.onclick =
function() {
//Do Something
}
};
}, 50);
最佳答案
根据我的说法,解决您问题的唯一方法是执行以下操作:
How do I handle a click anywhere in the page, even when a certain element stops the propagation?
并导入 jquery 库(如果还没有准备好的话)
关于javascript - 每次点击时调用函数(HTML 包括框架),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32226236/