代码之家  ›  专栏  ›  技术社区  ›  Tuğberk Kaan Duman

如何仅在某些锚上应用CSS

  •  0
  • Tuğberk Kaan Duman  · 技术社区  · 6 年前

    我有一个3页的网站。

    我想更改一些基于我所在位置的CSS。

    下面是一个例子,说明我在做什么,我得到什么,我期望什么。

    #header-outer #social-in-menu a i::before{
        color: #000000;
    }
    

    这确实会将社交按钮的颜色更改为黑色,但在每个锚上都是如此。我试着把它包起来 a[href*="features"] 这样,它只会改变功能链接,但社交图标仍然是白色的。

    这就是我努力实现的目标 show social icons black only in #features anchor .

    a[href*="features"] {
        #header-outer #social-in-menu a i::before{
            color: #000000;
        }
    }
    

    这个没有效果。什么都不改变。然而,CSS的第一部分将图标更改为黑色,但在所有锚定上。

    我怎样才能做到这一点?

    我试图得到的最终结果是:

    a[href*="features"] {
        #header-outer #social-in-menu a i::before{
            color: #000000; /* could be a different colour */
        }
    }
    
    
    a[href*="download"] {
        #header-outer #social-in-menu a i::before{
            color: #FFFFFF; /* could be a different colour */
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   dokgu    6 年前

    你可以这样做:

    $(document).ready(function() {
      $(document).on("click", "a", function(e) {
        var id = $(this).attr("href").substring(1);
        $(".parent").attr("id", id);
      });
    });
    a {
      color: black;
    }
    
    #features a {
      color: red;
    }
    
    #download a {
      color: blue;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="parent">
      <a href="#">Home</a>
      <a href="#features">Features</a>
      <a href="#download">Download</a>
    </div>

    这主要是更新 id 包装锚定的父元素的。此解决方案不使用 :target 包括使用javascript/jquery。