【问题标题】:Exclude child of specific class when jQuery automatically adds prettyPhoto class to linksjQuery自动将prettyPhoto类添加到链接时排除特定类的子类
【发布时间】:2013-03-22 22:03:52
【问题描述】:

我正在使用 jQuery 将 class="prettyPhoto" 添加到 <div class="entry-content"> 内部并包含图像的所有链接。

$('.entry-content a').has('img').addClass('prettyPhoto');

代码运行良好;但是我无法调整它以排除位于<div class="hover_link"> 中的任何链接,我不知道如何。

摆弄之后,我得到了以下结果:

if(!$('.entry-content a').parent('.hover_link')) {
    $('.entry-content a').has('img').addClass('prettyPhoto');
}

一些示例 html:

<div class="entry-content">

    <!-- prettyPhoto should be added to the next link -->
    <a href="whatever"><img src="whatever.jpg"></a>

    <!-- prettyPhoto shouldn't be added to the next link (there's no img) -->
    <a href="whatever">Some Text</a>

    <div class="hover_link">
        <!-- prettyPhoto shouldn't be added inside of .hover_link -->
        <a href="whatever"><img src="whatever.jpg"></a>
    </div>

</div>

【问题讨论】:

    标签: jquery image parent prettyphoto


    【解决方案1】:

    只需使用 jQuery 的 not

    $('.entry-content a').not('.hover_link a').addClass('prettyPhoto');
    

    【讨论】:

    • 谢谢!正是我需要的。我添加了 '.has("img")' 来制作:'$('.entry-content a').not('.hover_link a').has('img').addClass('prettyPhoto');'
    【解决方案2】:

    这样的事情应该可以工作:

    $('.entry-content a').has('img').each(function() {
      if($(this).parent('hover_link').length == 0) $(this).addClass('prettyPhoto');
    });
    

    【讨论】:

    • 谢谢!这对我也有帮助,因为我刚刚学到了一些东西。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2014-02-12
    • 1970-01-01
    相关资源
    最近更新 更多