Javascript 如何在MouseOver上编辑img src?
在一个有很多图片的网站上,我想在每个图片上添加鼠标悬停事件 我可以这样做:Javascript 如何在MouseOver上编辑img src?,javascript,jquery,Javascript,Jquery,在一个有很多图片的网站上,我想在每个图片上添加鼠标悬停事件 我可以这样做: <img class="brand" src="img/brands-grey/image1.svg" onmouseover="this.src='img/brands/image1.svg';" onmouseout="this.src='img/brands-grey/image1.svg';"/> <img class="brand" src="i
<img class="brand"
src="img/brands-grey/image1.svg"
onmouseover="this.src='img/brands/image1.svg';"
onmouseout="this.src='img/brands-grey/image1.svg';"/>
<img class="brand"
src="img/brands-grey/image2.svg"
onmouseover="this.src='img/brands/image2.svg';"
onmouseout="this.src='img/brands-grey/image2.svg';"/>`
`
但我宁愿从src中删除/插入“-grey”onmouseover/out。
有人能告诉我如何做到这一点吗
提前谢谢 您将jQuery作为标记,因此这里有一个jQuery解决方案:
$(document).ready(function(){
$('img.brand').mouseenter(function(){
$(this).attr('src', $(this).attr('src').replace('-grey', ''));
}).mouseleave(function(){
$(this).attr('src', $(this).attr('src').replace('brands', 'brands-grey'));
});
});
HTML将更加清晰:
<img class="brand" src="img/brands-grey/image1.svg" />
<img class="brand" src="img/brands-grey/image2.svg" />
我不会使用两组图像,而是使用一个现有的jQuery插件,如。您将jQuery作为标记,因此这里有一个jQuery解决方案:
$(document).ready(function(){
$('img.brand').mouseenter(function(){
$(this).attr('src', $(this).attr('src').replace('-grey', ''));
}).mouseleave(function(){
$(this).attr('src', $(this).attr('src').replace('brands', 'brands-grey'));
});
});
HTML将更加清晰:
<img class="brand" src="img/brands-grey/image1.svg" />
<img class="brand" src="img/brands-grey/image2.svg" />
我不会使用两组图像,而是使用一个现有的jQuery插件,如。您将jQuery作为标记,因此这里有一个jQuery解决方案:
$(document).ready(function(){
$('img.brand').mouseenter(function(){
$(this).attr('src', $(this).attr('src').replace('-grey', ''));
}).mouseleave(function(){
$(this).attr('src', $(this).attr('src').replace('brands', 'brands-grey'));
});
});
HTML将更加清晰:
<img class="brand" src="img/brands-grey/image1.svg" />
<img class="brand" src="img/brands-grey/image2.svg" />
我不会使用两组图像,而是使用一个现有的jQuery插件,如。您将jQuery作为标记,因此这里有一个jQuery解决方案:
$(document).ready(function(){
$('img.brand').mouseenter(function(){
$(this).attr('src', $(this).attr('src').replace('-grey', ''));
}).mouseleave(function(){
$(this).attr('src', $(this).attr('src').replace('brands', 'brands-grey'));
});
});
HTML将更加清晰:
<img class="brand" src="img/brands-grey/image1.svg" />
<img class="brand" src="img/brands-grey/image2.svg" />
我不会使用两组图像,而是使用现有的jQuery插件,比如。试试这个
<img class="brand" src="img/brands-grey/image1.svg" onmouseover="this.src='img/brands/image1.svg';" onmouseout="this.src='img/brands-grey/image1.svg';"/>
<img class="brand" src="img/brands-grey/image2.svg" onmouseover="this.src='img/brands/image2.svg';" onmouseout="this.src='img/brands-grey/image2.svg';"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
jQuery(function(){
jQuery('img.brand').hover(function(){
// this function will be called when the mouse in
var src= $(this).attr('src');
src.replace('brands-grey', 'brands') ;
$(this).attr('src', src);
},
function (){
// mouse out handler
var src= $(this).attr('src');
src.replace('brands', 'brands-grey');
$(this).attr('src', src);
}
)
})
</script>
jQuery(函数(){
jQuery('img.brand').hover(函数(){
//当鼠标进入时,将调用此函数
var src=$(this.attr('src');
src.替换(“品牌-灰色”、“品牌”);
$(this.attr('src',src);
},
函数(){
//鼠标移出处理程序
var src=$(this.attr('src');
src.替换(“品牌”、“灰色品牌”);
$(this.attr('src',src);
}
)
})
请更正jquery路径中的错误。这里我们使用了jQuery悬停方法。jsut按照您的要求从src中添加/删除灰色部分的逻辑。试试这个
<img class="brand" src="img/brands-grey/image1.svg" onmouseover="this.src='img/brands/image1.svg';" onmouseout="this.src='img/brands-grey/image1.svg';"/>
<img class="brand" src="img/brands-grey/image2.svg" onmouseover="this.src='img/brands/image2.svg';" onmouseout="this.src='img/brands-grey/image2.svg';"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
jQuery(function(){
jQuery('img.brand').hover(function(){
// this function will be called when the mouse in
var src= $(this).attr('src');
src.replace('brands-grey', 'brands') ;
$(this).attr('src', src);
},
function (){
// mouse out handler
var src= $(this).attr('src');
src.replace('brands', 'brands-grey');
$(this).attr('src', src);
}
)
})
</script>
jQuery(函数(){
jQuery('img.brand').hover(函数(){
//当鼠标进入时,将调用此函数
var src=$(this.attr('src');
src.替换(“品牌-灰色”、“品牌”);
$(this.attr('src',src);
},
函数(){
//鼠标移出处理程序
var src=$(this.attr('src');
src.替换(“品牌”、“灰色品牌”);
$(this.attr('src',src);
}
)
})
请更正jquery路径中的错误。这里我们使用了jQuery悬停方法。jsut按照您的要求从src中添加/删除灰色部分的逻辑。试试这个
<img class="brand" src="img/brands-grey/image1.svg" onmouseover="this.src='img/brands/image1.svg';" onmouseout="this.src='img/brands-grey/image1.svg';"/>
<img class="brand" src="img/brands-grey/image2.svg" onmouseover="this.src='img/brands/image2.svg';" onmouseout="this.src='img/brands-grey/image2.svg';"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
jQuery(function(){
jQuery('img.brand').hover(function(){
// this function will be called when the mouse in
var src= $(this).attr('src');
src.replace('brands-grey', 'brands') ;
$(this).attr('src', src);
},
function (){
// mouse out handler
var src= $(this).attr('src');
src.replace('brands', 'brands-grey');
$(this).attr('src', src);
}
)
})
</script>
jQuery(函数(){
jQuery('img.brand').hover(函数(){
//当鼠标进入时,将调用此函数
var src=$(this.attr('src');
src.替换(“品牌-灰色”、“品牌”);
$(this.attr('src',src);
},
函数(){
//鼠标移出处理程序
var src=$(this.attr('src');
src.替换(“品牌”、“灰色品牌”);
$(this.attr('src',src);
}
)
})
请更正jquery路径中的错误。这里我们使用了jQuery悬停方法。jsut按照您的要求从src中添加/删除灰色部分的逻辑。试试这个
<img class="brand" src="img/brands-grey/image1.svg" onmouseover="this.src='img/brands/image1.svg';" onmouseout="this.src='img/brands-grey/image1.svg';"/>
<img class="brand" src="img/brands-grey/image2.svg" onmouseover="this.src='img/brands/image2.svg';" onmouseout="this.src='img/brands-grey/image2.svg';"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
jQuery(function(){
jQuery('img.brand').hover(function(){
// this function will be called when the mouse in
var src= $(this).attr('src');
src.replace('brands-grey', 'brands') ;
$(this).attr('src', src);
},
function (){
// mouse out handler
var src= $(this).attr('src');
src.replace('brands', 'brands-grey');
$(this).attr('src', src);
}
)
})
</script>
jQuery(函数(){
jQuery('img.brand').hover(函数(){
//当鼠标进入时,将调用此函数
var src=$(this.attr('src');
src.替换(“品牌-灰色”、“品牌”);
$(this.attr('src',src);
},
函数(){
//鼠标移出处理程序
var src=$(this.attr('src');
src.替换(“品牌”、“灰色品牌”);
$(this.attr('src',src);
}
)
})
请更正jquery路径中的错误。这里我们使用了jQuery悬停方法。和jsut按照您的要求实现从src中添加/删除灰色部分的逻辑。您可以使用CSS伪类
:hover
并使用包含两个图像的任何包装器,这在所有情况下都具有更好的性能:
HTML:
现在,如果brands grey
只是一个灰显图像,您甚至不需要两个不同的图像,只需使用例如opacity:0.4代码>和“在图像上覆盖”将“不透明度”设置为1
或者使用带有转换的CSS3过滤器:您可以只使用CSS伪类:hover
并使用包含两个图像的任何包装器,这在所有情况下都具有更好的性能:
HTML:
现在,如果brands grey
只是一个灰显图像,您甚至不需要两个不同的图像,只需使用例如opacity:0.4代码>和“在图像上覆盖”将“不透明度”设置为1
或者使用带有转换的CSS3过滤器:您可以只使用CSS伪类:hover
并使用包含两个图像的任何包装器,这在所有情况下都具有更好的性能:
HTML:
现在,如果brands grey
只是一个灰显图像,您甚至不需要两个不同的图像,只需使用例如opacity:0.4代码>和“在图像上覆盖”将“不透明度”设置为1
或者使用带有转换的CSS3过滤器:您可以只使用CSS伪类:hover
并使用包含两个图像的任何包装器,这在所有情况下都具有更好的性能:
HTML:
现在如果品牌为灰色
i