小记 - javascript - 父页面监控子页面的点击事件

2019-07-15  本文已影响0人  木头就是我呀

简单记录,是可以监控的,例子如下:

父页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<iframe id="child" src="chi.html" scrolling="no" frameborder="0"></iframe>
</body>
<script>
    window.onload = function () {
        var _iframe = document.getElementById('child').contentWindow;
        var _div =_iframe.document.getElementById('button');
        _div.style.backgroundColor = 'red';
        _div.addEventListener("click",()=>{
            console.log("ssaad");
        })
    }
</script>
</html>

子页面:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
</head>
<body>
   <button id="button">111</button>
</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读