就是在两张图片之间来回切换,点一次按钮变一次。
<html>
<head>
<script language="javascript">
function dowith()
{
var img = document.getElementById("img");
img.src="2.jpg";
}
</script> </head>
<body>
<form name="form" action="" method="post">
<button type="button" style=" background:none;border:0;" onClick="dowith();"><img id="img" src="1.jpg" width="100"border="0" >
</button>
</form>
</body>
</html>
怎么改呢?想不好,新手
<html>
<head>
<script language="javascript">
function dowith()
{
var img = document.getElementById("img");
img.src="2.jpg";
}
</script> </head>
<body>
<form name="form" action="" method="post">
<button type="button" style=" background:none;border:0;" onClick="dowith();"><img id="img" src="1.jpg" width="100"border="0" >
</button>
</form>
</body>
</html>
怎么改呢?想不好,新手
<html>
<head>
<script language="javascript">
function dowith() {
var img = document.getElementById("img");
if (img.src == "2.jpg") { img.src = "1.jpg";
alert(img.src);
}
else {
img.src = "2.jpg";
alert(img.src);
}
}
</script>
</head>
<body>
<form name="form" action="" method="post">
<button type="button" style="background: none; border: 0;" onclick="dowith();">
<img id="img" src="1.jpg" width="100" border="0">
</button>
</form>
</body>
</html>我只是写了个样本。。img.src == "2.jpg"这里需要图片的真实路径。。自己改改吧。。
你用js写的话,最直观的就是直接if else判断,
当然高级一点,你可以这样写
<img src="images/1.gif" id="img1" /><script type="text/javascript">
var img1 = document.getElementById("img1");
img1.srcArr = ["images/1.gif", "images/2.gif"];
img1.clk=0;
img1.onclick = function() {
this.clk++;
this.src = this.srcArr[this.clk % 2];
}
</script>
试试
src2="http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/001/face/13.gif" />
<script type="text/javascript">
document.getElementById('img1').onclick = function() {
url = this.src;
this.src = this.getAttribute('src2');
this.setAttribute('src2', url);
}
</script>