jquery 双功能each
我不止一次拥有以下 HTML 代码块
<div id="page_1" class="page">
<div class="imageDetail_bg">
<img src="../_img/detail_car.jpg" alt="" id="car_detail" class="car_detail"/>
</div><!-- imageDetail-->
<div id="listThumbs">
<div id="thumbsContainer_1" class="thumbsContainer">
<div id="areaThumb" class="areaThumb">
<div id="posThumb_1" class="posThumb">
<img src="../_img/detail_car.jpg" class="detail_img" alt="">
</div>
</div><!--areaThumb-->
<div id="areaThumb" class="areaThumb">
<div id="posThumb_2" class="posThumb">
<img src="../_img/detail_car.jpg" class="detail_img" alt="" />
</div>
</div><!--areaThumb-->
...
...
...
</div><!--listThumbs-->
</div><!--page-->
和以下 jQuery 代码:
$('.page').each(function(i) {
$('.areaThumb').each(function(j) {
$('.detail_img').eq(j).click(function(){
$('.car_detail').eq(i).attr('src', $(this).attr('src'));
});
});
});
我想要做的是:对于每个页面都有一个拇指块,当我单击任何拇指时,#car_detail 中的图像将被替换为我点击的拇指的图像。目前我可以做到这一点,但是#car_detail 图像在所有页面中都被替换。我没有获得每个页面的单独操作。每次点击都会使该操作发生在所有页面中。
谁能告诉我我做错了什么? 谢谢
I have the following block of HTML code more than once
<div id="page_1" class="page">
<div class="imageDetail_bg">
<img src="../_img/detail_car.jpg" alt="" id="car_detail" class="car_detail"/>
</div><!-- imageDetail-->
<div id="listThumbs">
<div id="thumbsContainer_1" class="thumbsContainer">
<div id="areaThumb" class="areaThumb">
<div id="posThumb_1" class="posThumb">
<img src="../_img/detail_car.jpg" class="detail_img" alt="">
</div>
</div><!--areaThumb-->
<div id="areaThumb" class="areaThumb">
<div id="posThumb_2" class="posThumb">
<img src="../_img/detail_car.jpg" class="detail_img" alt="" />
</div>
</div><!--areaThumb-->
...
...
...
</div><!--listThumbs-->
</div><!--page-->
and the following jQuery code:
$('.page').each(function(i) {
$('.areaThumb').each(function(j) {
$('.detail_img').eq(j).click(function(){
$('.car_detail').eq(i).attr('src', $(this).attr('src'));
});
});
});
What I want to do is: For each page there's a block of thumbs, and when I click in any thumb, the image in #car_detail is replaced by the image of the thumb I clicked. At this moment I can do this, BUT the #car_detail image is replaced in all pages. I'm not getting individually actions for each page. Every click make the action occurs in all pages.
Can anyone tell me what I'm doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无需遍历 jquery 选择器结果的每个元素来绑定单击事件。
并且您缺少thumbsContainer div 的结束div,请在每个 之前添加它。
另外,如果您有一个 id car_detail 的元素,那么您应该使用
#car_detail
而不是.car_detail
工作示例 @ http://jsfiddle.net/2ZQ6b/
试试这个:
如果 .detail_img 元素用于 car_detail 图像,那么您可以简化上面的内容代码为:
You need not iterate through each element of the jquery selector result to bind a click event.
And you are missing a closing div for thumbsContainer div, add that before each .
Also if you have an element with id car_detail then you should use
#car_detail
instead of.car_detail
Working example @ http://jsfiddle.net/2ZQ6b/
Try this:
If the .detail_img elements are being used for the car_detail image then you can simplify the above code to:
您需要为您的子节点提供上下文:
每个this都指向由调用它的 jquery 函数给出的当前元素。
[编辑] Cybernate 找到了一种更好的方法来完成您想做的事情。我的回答主要解释了为什么你的代码没有按你想要的方式工作
You need to give context to your children nodes:
Every this is pointing to the current element given by the jquery function that called it.
[edit] Cybernate found a better way to do what you wanted to. My answer mostly explains why your code did not work as you wanted
我认为你对此有错误的方法,
你应该使用克隆,你会没事的...
HTML
JS
我认为这就是你应该做的,简单又优雅...不明白为什么你这么复杂:)
I think you have the wrong approach about this,
You should just use cloning and you will be fine...
HTML
JS
I think this is what you should be doing, simple and elegant... do not understand why you complicate as much :)