猿问

insertBefore插入图片,图片不显示。

<!doctype html>

<html>

<head>

    <meta charset="UTF-8">

    <title>首页弹出广告并消失</title>

</head>

<body>

    <h1>上方</h1>

    <p>上方会弹出一个图片</p>

    <script type="text/javascript">

        var img = document.createElement( "img" );

        img.src = "csdn.jpg";


        var h1 = document.gerElementsByTagName( "h1" )[0];

        document.body.insertBefore(img,h1);



    </script>

</body>

</html>


长风秋雁
浏览 843回答 1
1回答

互换的青春

拼写错误。应该是:getElementsByTagName()附完整代码:<!doctype html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <title>首页弹出广告并消失</title></head><body>&nbsp; &nbsp; <h1>上方</h1>&nbsp; &nbsp; <p>上方会弹出一个图片</p>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; var img = document.createElement("img");&nbsp; &nbsp; &nbsp; &nbsp; img.src = "csdn.png";&nbsp; &nbsp; &nbsp; &nbsp; var h1 = document.getElementsByTagName("h1")[0];&nbsp; &nbsp; &nbsp; &nbsp; document.body.insertBefore(img,h1);&nbsp; &nbsp; </script></body></html>页面展示效果如图:
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答