每次点击调用函数(HTML 包括帧)

Call function on every click (HTML includes frames)

我有一个函数应该在每次点击页面时调用。

所以我尝试在文档中添加一个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);

根据我的说法,解决你的问题的唯一方法是做这样的事情 post:
How do I handle a click anywhere in the page, even when a certain element stops the propagation?
并导入 jquery 库,如果你还没有的话