在Bootstrap侧边栏中隐藏特定元素

huangapple go评论71阅读模式
英文:

Hide specific elements in bootstrap sidebar

问题

以下是您提供的代码的翻译:

<div class="wrapper">
    <nav id="sidebar">
        <ul class="list-unstyled components">
            <li class="navhead">我的标题</li>
            <li><a href="#intro" class="sidebar">介绍</a></li>
            <li>
                <a href="#firstCategory" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle sidebar">第一类别</a>
                <ul class="collapse list-unstyled" id="firstCategory">
                    <li><a href="#firstCategory2" class="sidebar">介绍</a></li>
                    <li><a href="#firstItem" class="sidebar internal">第一项</a></li>
...
        <a id="firstItem"></a>
        <div id="firstItemDiv" class="internal">
...

根据您的描述,您以前使用了以下代码来在文档准备就绪时隐藏内部类($(document).ready(function ()),如果指定了URL参数:$('.internal').hide();。但是,这会导致DOM/页面闪烁,所以您在.css文件中添加了**.internal {display: none},并在ready时调用$('.internal').show();**,如果指定了参数。

换句话说,介绍总是显示,但只有在指定了URL参数时,第一项才应该显示。

这对于您的firstItemDiv很有效,但是我的侧边栏internal li元素(第一项)仍然显示。您如何让它默认不显示呢?

英文:

I have the following code

    &lt;div class=&quot;wrapper&quot;&gt;
        &lt;nav id=&quot;sidebar&quot;&gt;
            &lt;ul class=&quot;list-unstyled components&quot;&gt;
                &lt;li class=&quot;navhead&quot;&gt;My Title&lt;/li&gt;
                &lt;li&gt; &lt;a href=&quot;#intro&quot; class=&quot;sidebar&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
			&lt;li&gt;
				&lt;a href=&quot;#firstCategory&quot; data-toggle=&quot;collapse&quot; aria-expanded=&quot;false&quot; class=&quot;dropdown-toggle sidebar&quot;&gt;First Category&lt;/a&gt;
				&lt;ul class=&quot;collapse list-unstyled&quot; id=&quot;firstCategory&quot;&gt;
					&lt;li&gt;&lt;a href=&quot;#firstCategory2&quot; class=&quot;sidebar&quot;&gt;Intro&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href=&quot;#firstItem&quot; class=&quot;sidebar internal&quot;&gt;First Item&lt;/a&gt;&lt;/li&gt;
...
            &lt;a id=&quot;firstItem&quot;&gt;&lt;/a&gt;
            &lt;div id=&quot;firstItemDiv&quot; class=&quot;internal&quot;&gt;
...

I used to hide the internal class in my $(document).ready(function () with $('.internal').hide(); if a URL parameter was specified. However, this caused DOM/Page flicker, so I added .internal {display: none} to my .css file and now call $('.internal').show(); in my ready when the parameter is specified.

In other words, Intro always shows, but First Item should only show when the URL parameter is specified.

This works great for my firstItemDiv, but my sidebar internal li element (First Item) still shows. How can I get it not to show by default?

答案1

得分: 1

You should attach css also. If I understand correctly you change .internal (hiding or showing) and it works for id='firstItemDiv' and doesn't work for class="sidebar internal" because it uses some javascript probably...

Try to use

$(".internal').css("display", "none!important") instead of $(".internal").hide() and
$(".internal').css("display", "block!important") instead of show().

英文:

You should attach css also. If I understand correctly you change .internal ( hiding or showing ) and it works for id=&#39;firstItemDiv&#39; and doesnt work for class=&quot;sidebar internal&quot; because it uses some javascript propably...

Try to use

$(&quot;.internal&#39;).css(&quot;display&quot; , &quot;none!important&quot;) instead of $(&quot;.internal&quot;).hide() and
$(&quot;.internal&#39;).css(&quot;display&quot; , &quot;block!important&quot;) instead of show().

答案2

得分: 1

问题在于有一个更“具体”的规则覆盖了“内部”类规则。
有一个带有“id”特定性的规则:

#sidebar ul li a {
    display: block;
}

还有一个带有类特定性的规则:

.internal {
    display: none;
}

将第二个规则更改为:

#sidebar .internal, .internal {
    display: none;
}

应该可以解决这个问题。

英文:

(true confessions: I was able to peek at the actual page with the issue)

The issue here was that there was a more "specific" rule that was overriding the "internal" class rule.
There was a rule with "id" specificity:

#sidebar ul li a {
    display: block;
}

and another with class specificity:

.internal {
    display: none;
}

changing the second one to:

#sidebar .internal, .internal {
    display: none;
}

should fix it.

huangapple
  • 本文由 发表于 2020年1月3日 21:47:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定