jquery find() 按类图像

发布于 2024-11-17 08:32:00 字数 623 浏览 5 评论 0原文

我想找一张他的班级的照片。

我的代码是这样的:

<ul>
<li id='element_1'> <img src="images_1.png"  class="image_off"/></li>
<li id='element_2'> <img src="images_2.png"  class="image_off"/></li>
</ul>

现在,当我的鼠标位于 LI 或 LI 上时,我想选择内部图像。

我的 jquery 是这样的:

$("li").hover(

   var immagine = "img"
   ii= jQuery(this).find(immagine);
   $(ii).stop().animate({"opacity": "0"}, 500);

}

这工作正常。但是,如果我在同一个 LI 内有 2 个图像,并且我尝试使用他的类来选择图像:

var immagine = "img.image_off"

它不起作用......

你能帮助我吗?

I would like to find an image by his class.

My code is like this:

<ul>
<li id='element_1'> <img src="images_1.png"  class="image_off"/></li>
<li id='element_2'> <img src="images_2.png"  class="image_off"/></li>
</ul>

Now when my mouse will be orver th LI I would like to select the inner image.

My jquery is like:

$("li").hover(

   var immagine = "img"
   ii= jQuery(this).find(immagine);
   $(ii).stop().animate({"opacity": "0"}, 500);

}

This is workin fine. But if I have 2 images innner the same LI and I try to select the image by his class using:

var immagine = "img.image_off"

it is not working....

Can you help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2024-11-24 08:32:00

看起来你的范例有点混乱!

首先,您应该将一个函数传递给悬停方法。每次您悬停时都会运行该函数。

在悬停方法内,您可以通过传入选择器和上下文来进行上下文搜索。在这种情况下,this 将代表您将鼠标悬停在其上的 li

这会识别您想要进行手术的每个孩子。

这应该可以满足您的需要:

$("li").hover(function(){
    $("img.image_off",this).stop().animate({"opacity": "0"}, 500);
});

Looks like you have your paradigms a bit mixed up here!

First off, you should be passing a function to the hover method. That will run the function each time you hover.

Inside the hover method you can then do a contextual search by passing in a selector and a context. In this case this will represent the li that you hovered over.

This identifies each of the children you want to operate on.

This should do what you need:

$("li").hover(function(){
    $("img.image_off",this).stop().animate({"opacity": "0"}, 500);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文