LuyeeTom
V2EX  ›  问与答

CSS 隐藏元素后,如何在指定页面又可出现?

  •  
  •   LuyeeTom · Aug 27, 2021 · 1586 views
    This topic created in 1719 days ago, the information mentioned may be changed or developed.
    隐藏了某一模块,但在搜索结果页 123.com/?s=xx 又需要它出现
    .test {
    display:none;
    }

    该怎么办?能加条件吗?
    4 replies    2021-08-28 09:48:14 +08:00
    autoxbc
        1
    autoxbc  
       Aug 27, 2021   ❤️ 1
    非标准方法有 @document,不过只能用在 Firefox 上
    https://developer.mozilla.org/zh-CN/docs/Web/CSS/@document

    我的处理方法是在 js 里加这么一句
    document.body.setAttribute('location', location );

    然后就可以在 css 里进行地址查询
    body:not([location*="123.com/?s="]) .test { display: none; }
    P233
        2
    P233  
       Aug 27, 2021   ❤️ 1
    借助 js 的话,方法还是很多的

    纯 CSS 的话,就不那么优雅了:

    1. 给 body 一个 id,例如 #body
    2. url 加 hash, 如 123.com/?s=xxx#body
    3. 更新 CSS 为 `:target .test { display: block }`
    wdssmq
        3
    wdssmq  
       Aug 27, 2021
    .hidden {
    display: none;
    }

    <div class="test hidden">aa</div>

    在搜索而额外加一段

    <scripot>
    $("div.test").removeClass("hidden");
    </scripot>

    需要 JQ,虽然也有原生 JS 写法;

    也可以通过判断决定要不要加 hidden 类,然而要看具体的前、后端渲染。。
    orangejx
        4
    orangejx  
       Aug 28, 2021 via iPhone
    在搜索页加一段 css display:block
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3957 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 10:22 · PVG 18:22 · LAX 03:22 · JFK 06:22
    ♥ Do have faith in what you're doing.