简体   繁体   English

jQuery - 选择除特定子项之外的类

[英]jQuery - select class except specific child

I'm using the photo slider code from here: http://tympanus.net/codrops/2010/10/07/slider-gallery/ and trying to modify it so that the link below the images loads up in a new window. 我正在使用此处的照片滑块代码: http//tympanus.net/codrops/2010/10/07/slider-gallery/并尝试修改它,以便图像下方的链接在新窗口中加载。

For example: 例如:

<div class="content">
<div><a href="#"><img src="http://imageurl" alt="largeimageurl" /></a></div>
<div class="caption"><a target="_blank" href="image.php?id=someid&svc=somesvc" rel="nofollow">More Info</a></div>
</div>

I've tried a variety of different things with the javascript file, which currently reads: 我用javascript文件尝试了各种不同的东西,目前读取:

$fp_content_wrapper.find('.content').bind('click',function(e){
    var $current = $(this);
    //track the current one
    current = $current.index();
    //center and show this image
    //the second parameter set to true means we want to
    //display the picture after the image is centered on the screen
    centerImage($current,true,600);
    e.preventDefault();
    });

What I need to do is alter that first line so that it selects as normal, but doesn't apply anything to the caption div, which has the link in, so that it behaves as normal. 我需要做的是更改第一行,以便它正常选择,但不会对带有链接的标题div应用任何内容,以使其行为正常。

I know there's similar questions on SO, but I have tried most of the suggested ones. 我知道在SO上有类似的问题,但我已经尝试了大多数建议的问题。 I don't have much experience with JS at all, so it could easily be me doing it wrong. 我对JS没有多少经验,所以很容易让我做错了。

Appreciate any help! 感谢任何帮助!

Try this: 尝试这个:

$fp_content_wrapper.find('.content>*:not(.caption)').bind('click',function(e) .....

What was happening is that you were selecting the content tag, instead you wanted to select what was inside it, except for anything with the .csption class. 发生的事情是你选择了内容标签,而不是你想要选择内部标签,除了.csption类的任何东西。 '>' means element directly below and '*' means select all. '>'表示直接位于下方的元素,'*'表示全选。 So what it translates to is "select everything directly below .content that is not a class of .caption". 所以它转化为“直接选择.content之下的所有内容。不是一个.caption类”。

If you continue using jquery it's worth while studying the selectors. 如果你继续使用jquery,那么在研究选择器时它是值得的。

Good luck 祝好运

尝试:

$fp_content_wrapper.find('.content:not(.caption)').bind("click", ...

Try this James 试试这个詹姆斯

$fp_content_wrapper.find('.content').bind('click',function(e){     
   if(!$(this).attr('class'))   
   {
     var $current = $(this);     
     //track the current one     
     current = $current.index();     
     //center and show this image     
     //the second parameter set to true means we want to     
     //display the picture after the image is centered on the screen     
     centerImage($current,true,600);     
     e.preventDefault();  
   }
}); 

There is a neater way to do this but this should do. 有一个更简洁的方法来做到这一点,但这应该做。 This should make sure that that only the div that doesn't have a class runs the function. 这应该确保只有没有类的div运行该函数。 Obviously be careful or change the logic if you want to use a class on the other div. 如果你想在另一个div上使用一个类,显然要小心或改变逻辑。

Cheers 干杯

EDIT: 编辑:

After looking over your code again you could also test the hyperlink within the div to check whether it has an alt attribute or what the alt attribute is. 再次查看代码后,您还可以测试div中的超链接,以检查它是否具有alt属性或alt属性。 This will be a more specific test. 这将是一个更具体的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM