【问题标题】:jQuery - select class except specific childjQuery - 选择除特定孩子之外的类
【发布时间】:2012-05-10 01:06:00
【问题描述】:

我正在使用此处的照片滑块代码:http://tympanus.net/codrops/2010/10/07/slider-gallery/ 并尝试对其进行修改,以便图像下方的链接在新窗口中加载。

例如:

<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>

我已经用 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();
    });

我需要做的是更改第一行,使其正常选择,但不对包含链接的标题 div 应用任何内容,使其行为正常。

我知道关于 SO 有类似的问题,但我已经尝试了大多数建议的问题。我根本没有太多的 JS 经验,所以很容易是我做错了。

感谢任何帮助!

【问题讨论】:

  • 标题 div 的 html 代码是什么?如果您的意思是 id,您可以使用 $fp_content_wrapper.find('.content').not('#caption').bind('click',function(e){ 或将 # 更改为 .(如果它是一个类)
  • 代码在问题的第一个代码块中,嵌套在内容 div 中。我尝试将标题 div 放在此之外,但它会导致更多的样式工作。最终可能会更容易做到这一点。
  • 你说的太对了,我怎么没看到!
  • 我仍然感谢您的帮助 :-)
  • 我已经为你添加了答案。请让我知道这可不可以帮你。干杯

标签: javascript jquery select except


【解决方案1】:

试试这个:

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

发生的情况是您选择的是内容标签,而不是您想要选择其中的内容,除了 .csption 类的任何内容。 '>' 表示正下方的元素,'*' 表示全选。所以它的意思是“选择.content 正下方不是.caption 类的所有内容”。

如果你继续使用 jquery,学习选择器是值得的。

祝你好运

【讨论】:

  • 太棒了,按预期工作(很遗憾我们今天早上不得不演示系统,但它不工作:-/)当然感谢答案,这是我可以在未来的实现中使用的东西这。再次感谢!你能解释一下这个改动,以便我理解它的含义吗?
  • 我已经给你做了解释
  • 非常感谢,我现在明白了很多。
【解决方案2】:

试试这个詹姆斯

$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();  
   }
}); 

有一种更简洁的方法可以做到这一点,但应该这样做。这应该确保只有没有类的 div 运行该函数。如果你想在另一个 div 上使用一个类,显然要小心或更改逻辑。

干杯

编辑:

再次查看您的代码后,您还可以测试 div 中的超链接以检查它是否具有alt 属性或alt 属性是什么。这将是一个更具体的测试。

【讨论】:

  • 感谢您 - 不幸的是仍然无法正常工作。我认为该功能需要应用于主要内容 div,而不仅仅是内部 div(没有类的那个)。与此类似的代码是什么来检查类是否等于“标题”?所以,if(class != 'caption') {code} 是一种伪代码
【解决方案3】:

试试:

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

【讨论】:

  • 试过这个,但不幸的是对标题链接没有影响。两者仍会启动较大版本的图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 2014-01-01
相关资源
最近更新 更多