【问题标题】:Apply style to grandparent of the grandchild with specific value [duplicate]将样式应用于具有特定值的孙子的祖父母[重复]
【发布时间】:2014-10-10 11:16:28
【问题描述】:

我有以下代码:

<div class="photos-wrapper" id="detailPhoto">
    <div class="pseudo">
        <a href="#/123456/">fixedTEXT</a>
    </div>
    <div class="image-wrapper">
       <a href="#"></a>
    </div>
    <div class="activites">
      <a href="#"></a>
    </div>
    <div class="commentaire">
        <a href="#"></a>
    </div>
</div>

我想在第一个和主要的&lt;div class="photos-wrapper" id="detailPhoto"&gt; 中包含我自己的 CSS 样式,但唯一的方法是识别孙选择器,即&lt;a href="#/123456/"&gt;,因为有多次出现相同的代码。 也许当我展示我尝试过的东西时会更清楚一点:

a[href*="123456"] > div.pseudo > div.photos-wrapper[id^="detailPhoto"] {
    display: none !important;
}

div.photos-wrapper[id^="detailPhoto"] < div.pseudo < a[href*="123456"] {
    display: none !important;
}

这就是我尝试这样做的方式,但它显然不起作用。 我可能在这里尝试做的事情称为父选择器,但我不太确定。

@编辑 让我们看一下这段代码,它实际上更详细: http://jsfiddle.net/60ezqtL7/

目标是通过 display: none; 样式隐藏包含完全相同值的整个 div &lt;a href="#/000000/"&gt;PHOTO 1&lt;/a&gt;

【问题讨论】:

  • CSS 在一个方向上工作,你可以选择一个孩子或后代,而不是父母或祖先。有一个提议的父选择器作为 CSS4 的一部分,但由于 CSS3 仍处于草案阶段,所以不要等待它。它可以用 javascript 完成,只需 jQuery 库
  • 附带说明,为什么DIV[class="photos-wrapper"] 而不是div.photos-wrapper
  • 这正是我尝试过的,但在 html 代码中,就像我说的那样,同一部分代码多次出现。
  • 是的,鉴于您的更新,您将需要一些 javascript。我会看看我能想出什么。您是要隐藏所有具有相同值的 div,还是显示第一个并隐藏其他 div?
  • 附带说明,如果这些是由某些服务器端代码生成的,那么处理那里的重复项会好得多。

标签: html css css-selectors parent-child


【解决方案1】:

在这种情况下(或许多其他情况)不需要使用 jQuery。

detailPhoto.classList.toggle('hide', detailPhoto.querySelector('[href=#/123456]'))

【讨论】:

  • 谢谢,我不知道classList 上的toggle 方法。今天是学习新东西的好日子。
  • 虽然第二次看,不如我想象的那么好,因为不幸的是 IE 8 和 9 仍然相关:developer.mozilla.org/en-US/docs/Web/API/Element.classList
  • 感谢您的帮助,但不幸的是,所有这些解决方案似乎都不起作用。我编辑了我的第一篇文章并添加了一些更详细的代码。
【解决方案2】:

正如我在对您的回答的评论中提到的,没有父母或祖先选择。通过 jQuery 最简单、最有效的方法是 has() method

$('#detailPhoto').has('a[href*="123456"]').hide(); // or use .addClass() instead

为你使用Google to host jquery

Demo : 我在演示中使用了类选择器,因为 id 应该是唯一的。

addClass Demo

更新

鉴于您的更新并假设您想显示每张照片中的 1 张且仅显示 1 张,带有相同 href 的照片的其他包装器将被隐藏。

/*Loop through each link in  div with cass psudo 
  in a div with class photos-wrapper*/
var found = {};
$(".photos-wrapper .pseudo a").each(function(){
    var $this = $(this);
    var href = $this.attr("href");
    //if the href has been enountered before, hide the .photos-wrapper ancestor    
    if(found[href]){
        $this.closest(".photos-wrapper").hide();
        /*Other options: 

        Use Css direct
        $this.closest(".photos-wrapper").css("display", "none");

        Assign a duplicate class, then style that class ass appropriate
        $this.closest(".photos-wrapper").addClass("duplicate");
       */
    }else{
    //otherwise add it to the array of what has been found
        found[href] = true;
    }
});

Demo

如果您不熟悉 jquery,请务必阅读它的实现方式以及$(document).ready(); 的目的

更新 2

要隐藏所有具有复制 href 的容器,请使用:

//Loop through each a tag 
$(".photos-wrapper .pseudo a").each(function () {

    var $this = $(this);
    //Get the href
    var href = $this.attr("href");

    //Check if more than one exists
    if ($('.photos-wrapper .pseudo a[href="' + href + '"]').size() > 1) {
        //Hide all .photo-wrapper containers that have  the replicated href
        $('.photos-wrapper .pseudo a[href="' + href + '"]').closest(".photos-wrapper").hide();
    }
});

Another Demo

如果可能的话,我仍然建议在服务器端删除重复项。

总的来说,&lt;center&gt; 标签在 HTML4 中已被贬低,不应再使用。改用 CSS。有很多关于如何使用 CSS 使内容居中的示例。

【讨论】:

  • 我没有使用 Maxthon 的经验,jquery 非常适合跨浏览器兼容,所以如果这是问题,我会感到惊讶,我最新的演示小提琴对你有用吗?
  • 您好,再次感谢,但任何给定的解决方案似乎都不起作用,这是怎么回事?这与maxthon浏览器完全兼容吗?我在maxthon版本:4.3.1.1000,我从来没有遇到过这种问题。
  • 在这个演示中没有发生任何事情。它应该做什么?
  • 我刚刚下载了 Maxthon,jsfiddle.net/60ezqtL7/1 工作正常,只有 1 个实例“照片 1”而不是 2。将其与 :jsfiddle.net/60ezqtL7/2 进行比较。页面加载时会发生任何事情,因此您看不到任何事情发生。
  • 另见:jsfiddle.net/60ezqtL7/3 在按钮点击时隐藏重复项。相同的逻辑,只是在您单击某些内容而不是页面加载时发生。
【解决方案3】:

目前,仅使用 CSS 无法做到这一点,但您可以使用 JQuery 轻松做到这一点。这将搜索#detailPhoto 的后代并隐藏href(将其设置为显示:无;)。

<script>
$(function() {
    $('#detailPhoto').find('a[href$="#/123456/"]').hide();
});
</script>

要搜索父母,你会使用这个。

<script>
$(function() {
    $('a[href$="#/123456/"]').closest('#detailPhoto').hide();
});
</script>

要使用它,您还需要将 JQuery 库添加到文档的头部。

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

【讨论】:

  • 感谢您的帮助,但不幸的是我无法完成这项工作。请查看我的第一篇文章,我对其进行了编辑并添加了一些更详细的代码。
猜你喜欢
  • 2018-09-04
  • 2015-07-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 2015-08-30
  • 2018-01-13
  • 2013-06-29
相关资源
最近更新 更多