javascript 实现简单的点击按钮图片切换
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img{
float: left;
position: absolute;
top: 50px;
left: 0;
}
</style>
</head>
<body>
<button id="m">猫</button>
<button id="lh">老虎</button>
<img src="images/猫.png" alt="" title="猫">
<script>
var m = document.getElementById('m');
var lh = document.getElementById('lh');
var img = document.querySelector('img');
lh.οnclick=function(){
img.src="images/老虎.png";
img.title="老虎";
}
m.οnclick=function(){
img.src="images/猫.png";
img.title="猫";
}
</script>
</body>
</html>
效果: