[英]Traversing the DOM Tree jquery
A fairly simple traversal that I can't seem to figure out... 我似乎无法找出一个相当简单的遍历...
Basically if someone clicks on the <li>
and I want to change the image in the first div, how would I go about doing that? 基本上,如果有人单击
<li>
并且我想在第一格中更改图像,我该怎么做? tried a few things including : 尝试了一些事情,包括:
$(this).closest('.currentImage img')
<div class="imageSlider">
<div class="currentImage">
<img src="LP5/lp5_1.png"/>
</div>
<ul class="thumbnails">
<li><img src="LP5/lp5_1.png"/></li>
<li><img src="LP5/lp5_2.png"/></li>
<li><img src="LP5/lp5_3.png"/></li>
<li><img src="LP5/lp5_4.png"/></li>
<li><img src="LP5/lp5_5.png"/></li>
</ul>
</div>
Jquery jQuery的
$('.thumbnails li').click(function(e){
e.preventDefault();
var newImage = $(this).children('img').attr('src');
var $image = $(this).closest('.currentImage img');
$image.fadeOut(500, function() {
$image.attr('src', newImage);
}).fadeIn(500);
});
Try finding the parent .imageSlider
element, and then finding its child .currentImage
element. 尝试找到父
.imageSlider
元素,然后找到其子.currentImage
元素。
$('.thumbnails li').click(function(e){
e.preventDefault();
var newImage = $(this).children('img').attr('src');
var $image = $(this).parents('.imageSlider').find('.currentImage img');
console.log(newImage);
$image.fadeOut(500, function() {
$image.attr('src', newImage);
}).fadeIn(500);
});
Another option would be $(this).parent('ul').siblings('.currentImage').find('img')
. 另一个选择是
$(this).parent('ul').siblings('.currentImage').find('img')
。
That selector isn't exactly right. 该选择器并不完全正确。
.currentImage
is a sibling of the current element's parent (ie .thumbnails
), so closest()
will never find it. .currentImage
是当前元素的父元素(即.thumbnails
)的同级元素,因此.thumbnails
closest()
永远找不到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.