如何用JS语句显示图片?

JavaScript024

如何用JS语句显示图片?,第1张

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<script>标签,输入js代码:$('body').append('<img src="https://www.baidu.com/img/bd_logo1.png" />')。

3、浏览器运行index.html页面,此时图片被js成功显示到页面上。

直接系在innerHtml中只能显示为文本的

应该把img添加到DOM中去才可以显示

参考下面的代码

 window.onload = function () {

             var o = document.getElementById("C_Top")

             var img = document.createElement("img")

             img.src = "

https://www.baidu.com/img/bdlogo.png

"

             var b = document.getElementById("bin2")

             b.onclick = function () {

                 o.appendChild(img)

             }

         }

这样才能把img元素添加到document中