img src 路径仅在悬停 jquery 时给出第一个子项
我在 html 中创建了一个图像 div,下面是代码
<div class="container">
<div class="TabMenu">
<span><img src="images/ecom.png"></span>
<span><img src="images/cms.png"></span>
<span><img src="images/crm.png"></span>
<span><img src="images/creative.png"></span>
<span><img src="images/development.png"></span>
<span><img src="images/qa.png"></span>
<span><img src="images/mobile.png"></span>
</div>
</div>
现在我编写了 jquery
$(".container .TabMenu span").mouseover(function() {
var imgpath = $('img').prop('src');
alert(imgpath);
})
当鼠标悬停在任何图像上时,它会显示一个带有第一个图像路径的警报框,但不显示相应的路径。我如何检索各自的图像路径
现在我在这里扩展一个问题
当我单击特定图像时,它的图像应该被更改,下面是我编写的代码
我有两个图像作为 image1.png 和 image1-h.png,通过这样我使用悬停样式,但
$(".container .TabMenu span").click(function(){
var newSrc = $('img', this).attr('src').replace(".", "-h.");
$('img', this).attr("src", newSrc);
});
我有两个图像作为 image1.png 和 image1-h.png,通过这种方式,我通过将图像名称替换为“-h”来使用悬停样式。但是当我单击此图像 ecom 时,它会变成 ecom-h,cms 会变成 cms-h,我怎样才能恢复该图像的原始 src 路径,以便该图像处于活动状态。 当我单击其他图像时,先前单击的图像的路径应到达原始路径
I have created a image div in html and below is the code
<div class="container">
<div class="TabMenu">
<span><img src="images/ecom.png"></span>
<span><img src="images/cms.png"></span>
<span><img src="images/crm.png"></span>
<span><img src="images/creative.png"></span>
<span><img src="images/development.png"></span>
<span><img src="images/qa.png"></span>
<span><img src="images/mobile.png"></span>
</div>
</div>
And now i written jquery
$(".container .TabMenu span").mouseover(function() {
var imgpath = $('img').prop('src');
alert(imgpath);
})
And when hover on any image, its displaying an alertbox with path of first image, but not the respective path. How can i retrieve respective image paths
And Now I am extending a question here itself
When i click on particular image its image should be changed and below is the code i wrote
I have two images as image1.png and image1-h.png, by this way i am using hovering style but
$(".container .TabMenu span").click(function(){
var newSrc = $('img', this).attr('src').replace(".", "-h.");
$('img', this).attr("src", newSrc);
});
I have two images as image1.png and image1-h.png, by this way i am using hovering style by replacing the image name to "-h." but when i click this image ecom then its turns into ecom-h and cms to cms-h, how can i revert the original src path of that image so that, this image is active.
When i click on other image, previously clicked image should path come to original path
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请使用此选项:
在您的情况下,您正在整个文档中查找
img
标记并首先获取它们。Use this instead:
In your case you are looking for a
img
tag in the whole document and taking first of them.使用
this
定位当前元素,并使用children()
< /a> 选择图像;Use
this
to target the current element, andchildren()
to select the image;尝试:
Try: