点击按钮可以更换图片和图片标签
<body>
<button id="pq">佩奇</button>
<button id="qz">乔治</button>
<img src="img/peiqi.jpg" alt="">
<script>
//获取元素
var pq = document.getElementById('pq');
var qz = document.getElementById('qz');
var img = document.querySelector('img');
//注册事件
pq.onclick = function() {
img.src = 'img/peiqi.jpg';
img.title = '佩奇';
}
qz.onclick = function() {
img.src = 'img/qiaozhi.jpg';
img.title = '乔治';
}
</script>
网友评论