<div id="zuo3">
<img name="img" onmouseover="bigger()" onmouseout="smaller()" src="img/1.jpg"
style="cursor:pointer;width:100px;height:100px;" />
<img name="img" onmouseover="bigger()" onmouseout="smaller()" src="img/2.jpg"
style="cursor:pointer;width:100px;height:100px;" />
<script type="text/javascript">
var img = document.getElementsByName('img');
function bigger(){
img.name.width = '200px';
img.name.height = '200px';
}
function smaller(){
img.name.width = '90px';
img.name.height = '100px';
}
</script>
</div>
这是我的网页里的代码 我想把鼠标移到图片上的时候,就对图片进行放大,但是不知道为什么这样做会报错 我没怎么学习过js 所以请求大神帮忙解决一下
你获取的图片对象是一组,应该是写一个事件,每个图片都去调用。
var img = document.getElementsByName('img');返回的应该是个数组吧
你用 for循环看看
for(var i=0;i<img.length;i++){
img[i].name.width = '200px';
img[i].name.height = '200px';
}
把图片路径换成你的试一下看看吧。
<div>
<img src="1.jpg" width="200" height="200" alt="图片1" />
<img src="1.jpg" width="200" height="200" alt="图片2" />
</div>
<script type="text/javascript">
window.onload=function(){
var imgs = document.getElementsByTagName("img");
for(var i = 0;i<imgs.length;i++){
imgs[i].onmouseover=function(){
this.setAttribute('width','300');
this.setAttribute('height','300');
}
imgs[i].onmouseout=function(){
this.setAttribute('width','200');
this.setAttribute('height','200');
}
}
};
</script>
你用 for循环看看
for(var i=0;i<img.length;i++){
img[i].name.width = '300px';
img[i].name.height = '300px';
}
建议用js编辑器1st JavaScript Editor Pro写,我用这个写的基本都正常
style="cursor:pointer;width:100px;height:100px;" />
function bigger(obj){
obj.style.width = '200px';
obj.style.height = '200px';
}
function smaller(obj){
obj.style.width = '90px';
obj.style.height = '100px';
}
这么写
onmouseover="bigger(this)" onmouseout="smaller(this)"
这是用你原来的思路实现的,上面还有用我写的另外一种方法实现的
记得把图片路径换成你的
<div id="zuo3">
<img name="img" width="100" height="100" onmouseover="bigger(this)" onmouseout="smaller(this)" src="1.jpg" />
<img name="img" width="100" height="100" onmouseover="bigger(this)" onmouseout="smaller(this)" src="1.jpg" />
<script type="text/javascript">
function bigger(img){
img.width = '200';
img.height = '200';
}
function smaller(img){
img.width = '100';
img.height = '100';
}
</script>
<script type="text/javascript">
var img = document.getElementsByName('img');
function bigger(){
img[0].style.width = "200px"
img[0].style.height= "200px"
}
function smaller(){
img[0].style.width = '90px';
img[0].style.height = '100px';
}
</script>
因为 用 var img = document.getElementsByName('img'); 取到的是数组,img[0]是第一张图片,同理img[1]第二张;同时 img[0].style.width=xx是修改样式
不过是写的CSS的,在360、FireFox、Chrome、世界之窗测试都可以,你试试
http://blog.csdn.net/xtuwz/article/details/78966159
http://www.htmleaf.com/jQuery/Image-Effects/201505071793.html
用插件啦jq这个插件不知多好用
第一点的话,你使用getElementsByName,那么我们把它打印出来,会看到是一个数组,
第二点,你在使用的时候是这样的img.name,这儿的话是第一个错误,因为img是一个数组。所以你可能要用img[0].name区获取单独的对象
第三点。貌似你获取name属性也咩啥用的样子呀。。。。因为你的width作用的是数组
正确答案怎么做,很多人大都给出了,我也没去看。如果还没解决的话q:1738731967
var img = document.getElementsByName('img');
这句选取了所有name='img'的节点(例子中有2个)
而function内部直接操作img,它不知道你要操作哪个img
img.name.width = '200px';
解决方法:
1.如果只有少量图片,你可以用document获取每个节点,分别进行操作,
2.如果多个图片用到这个方法,有个 .this方法 是自动选择你所触发的节点.
有问题可以随时追问,乐意彼此之间交流,如果对你有帮助,请好评,点赞,谢谢!
以上代码已验证(请放心使用)
注意:
1:script块代码可以放在head或body里面,但是script块代码和div代码是相互独立的,你的script代码放在了div里面,应该拿出来
2:此处对象是多张图片,需要用this,和function XXX(obj) 定位到当前对象
有问题可以随时追问,乐意彼此之间交流,如果对你有帮助,请好评,点赞,谢谢!