<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<img src="../images/f.jpg" alt="青蛙" id="p1"/>
<button onclick="changeIt()">改变</button>
<button onclick="changeIt2()">改变2</button>
</body>
<script type="text/javascript">
// html方式改变
function changeIt(){
var p1 = document.getElementById("p1");
p1.src = "../images/k.jpg";
}
// dom 方式改变
function changeIt2(){
var p1 = document.getElementById("p1");
console.log(p1.getAttribute("alt"));
// 获取元素属性
p1.setAttribute("src","../images/k.jpg");
}
</script>
</html>
网友评论