html中有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"/>
<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>

image.png

阅读 1.7k
1 个回答

标签没闭合<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>
推荐问题
宣传栏