html中有iframe时按钮的点击事件提示函数不存在?

作者站长头像
站长
· 阅读数 10
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<button onclick="takeScreenshot()">点我</button>
<iframe id="main" src="http://www.chunshu.net" width="300" height="200"/>
<script type="text/javascript">
        function takeScreenshot() {
            console.log("sss")
            /*let iframe = document.getElementById('main')
            let iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
            if (iframeWin.document.body) {
                console.log(iframeWin.document.body)
            }*/
        }
</script>
</body>
</html>

html中有iframe时按钮的点击事件提示函数不存在?

回复
1个回答
avatar
test
2024-07-03

标签没闭合<iframe/>:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<button onclick="takeScreenshot()">点我</button>
<iframe id="main" src="http://www.chunshu.net" width="300" height="200"></iframe>
<script type="text/javascript">
        function takeScreenshot() {
            console.log("sss")
            /*let iframe = document.getElementById('main')
            let iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
            if (iframeWin.document.body) {
                console.log(iframeWin.document.body)
            }*/
        }
</script>
</body>
</html>
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容