展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>放大和缩小</title>
<script type="text/javascript">
window.onload = function(){
<!-- 获取图片对象要放在window.onload之中,否则无法获取图片对象;或者将script放在图片之后例如image2 -->
var image = document.getElementById("testImg");
image.onclick = function() {
var width = parseInt(this.width);
if(width == 750) {
this.width = 200;
this.height = 180;
} else {
this.width = 750;
this.height = 536;
}
}
};
</script>
</head>
<body>
<img id="testImg" src="../images/2139491297979223579.jpg" width="750" height="536" alt="长江" />
<img id="testImg2" src="../images/7666415_143721636125_2.jpg" width="1024" height="685" alt="黄河" />
<script type="text/javascript">
<!-- 由于页面在加载到此处时,图片已经加载完成,所以不需要放在window.load中 -->
var image2 = document.getElementById("testImg2");
image2.onclick = function() {
var width = parseInt(this.width);
if(width == 1024) {
this.width = 512;
this.height = 340;
} else {
this.width = 1024;
this.height = 685;
}
};
</script>
</body>
</html>
追问
试了,点击后图片就没了
追答
都说了你的代码有问题,长宽不是像你那样赋值的。而且width与height属性也不属于style
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询