用setAtrribute()方法
举个例子
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<img src="1.jpg" alt="">
<script>
//获取到图片标签标签
var img = document.getElementsByTagName("img")[0]
//设置图片路径
img.setAttribute("src","2.jpg")
</script>
</body>
</html>
这里采用jquery的(也可以参照改成js的),直接浏览器运行即可执行:<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript"src="
<script>
$(function(){
$("#img").attr("src","/img/2.jsp")
//用于前台展示
$("span").html($("#img").attr("src"))
})
</script>
</head>
<body>
目前路径:<span>/img/1.jpg</span><img id="img" src="/img/1.jpg" />
</body>
</html>