如何用JS实现简单的图片替换

JavaScript07

如何用JS实现简单的图片替换,第1张

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

1、首先,打开html编辑器,新建html文件,例如:index.html,填写问题基础代码。

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

3、浏览器运行index.html页面,此时网页上的所有图片都被替换成了指定的图片。

let str='asdfsd<span>aaaa234</span>fdsad2342332'

str=str.split('<span>').map(function(e){

let a=e.split('</span>')

a[a.length-1]=a[a.length-1].replace(/a/g,'<span>2222</span>')

return a.join('</span>')

}).join('<span>')

console.log(str)