简体   繁体   English

<div>块的CSS样式在display = none然后display = block上更改

[英]A <div> block's CSS styles change on display=none then display=block

Here is a div block that appears fine as part of a top row of 5 nearly-identical divs that make up a Nav bar at the top of my page -- although there are 5 of the divs like the one I show here with an anchor, I'm only showing the one that goes on the far right end of the Nav bar for my "User Dashboard" link: 这是一个div块,看起来很好,作为顶部5个几乎相同的div的一部分组成了我页面顶部的导航栏 - 虽然有5个div像我在这里展示的那个div ,我只是在我的“用户仪表板”链接上显示导航栏最右端的那个:

<div class="pageTopRowContainer"> // this is the containing div for all 5 of the Nav bar divs

    // not shown here are the first 4 identical-except-in-name-and-linked-page divs 
    //    with <a> anchors.......
    // And here's the right most div-with-<a>-anchor on my Nav bar:

    <div class="pageTopRowContainerLabel" id="UserAccountNavbarLinkId">
        <a class="pageTopRowTextStyle"
           href="http://localhost/ServiceMain/UserDashboard.php">User Dashboard</a>
    </div>
</div>

Here are the CSS styles: 以下是CSS样式:

.pageTopRowContainer
{
    display:inline-block;
    width:100%;
    font-family: Arial, sans-serif;
    font-weight: bold;
}


.pageTopRowContainerLabel
{
  display:inline-block;
  background-color: green;
  color: RGB(255,255,255);
  padding-top: 2px;
  padding-bottom:2px;
  padding-left:5px;
  padding-right:5px;
}


.pageTopRowTextStyle
{
    color:RGB(255,255,255); 
    text-decoration:none;
}

All 5 divs-with-anchors appear fine: left-to-right in one row in my Nav bar, each div as block with a green background wide enough for the text label I have in each div. 所有5个div-with-anchors看起来很好: 我的导航栏中的一行从左到右,每个div作为块,绿色背景足够宽,可以放入每个div中的文本标签。

The div that's giving me trouble is the one above, the one with the text label of "User Dashboard". 给我带来麻烦的div是上面的那个,文本标签为“User Dashboard”的那个。 It normally appears fine as the rightmost 5th div on the Nav bar. 它通常看起来很好,作为导航栏上最右边的第5个div。

I need to hide this rightmost div until the user logs in. 我需要隐藏最右边的div,直到用户登录为止。

So I added javascript (below) to show/hide the div above based on a session variable of 'login status.' 所以我添加了javascript(下面)来根据“登录状态”的会话变量显示/隐藏上面的div。

And this showing/hiding logic works in the sense that it shows and hides that rightmost div "User Dashboard" as the user logs in and logs out. 这个显示/隐藏逻辑的工作原理是,当用户登录并注销时,它显示并隐藏了最右边的div“User Dashboard”。

EXCEPT instead of a rectangle on the right end of the Nav bar with a green background wide enough for the text label "User Dashboard" -- when the 'display=block' javascript executes -- the "User Dashboard" (previously rightmost) div -- now appears below the other 4 Nav bar divs and its green background is as wide as the browser with the "User Dashboard" text left-justified. 除了导航栏右端的矩形外,绿色背景宽度足以容纳文本标签“用户仪表板” - 当'display = block'javascript执行时 - “用户仪表板”(以前最右边)div - 现在显示在其他4个导航条div 下方 ,其绿色背景与浏览器一样宽,“用户仪表板”文本左对齐。

I don't see how this could be a 'float' issue -- take another look at the div declaration above. 我不知道这可能是一个“浮动”问题 - 再看看上面的div声明。 That div declaration appears exactly like the other 4 divs on the Nav bar that are to its left -- AND before I added the Javascript below, all 5 divs appeared left-to-right in one row in my Nav bar, each div as block with a green background wide enough for the text label I have in each div. div声明的显示方式与导航栏左侧的其他4个div 完全相同 - 在我添加下面的Javascript之前,所有5个div在我的导航栏中从左到右显示,每个div作为块绿色背景足够宽,我在每个div中都有文本标签。

Here's the showing/hiding javascript code -- it only applies to the rightmost div on the navbar (above) -- if the user is logged in they need to see the above 'User Dashboard' div and if not logged in I set display=none: 这是显示/隐藏的javascript代码 - 它只适用于导航栏上最右边的div(上图) - 如果用户登录后需要查看上面的“用户仪表板”div,如果没有登录,我设置display =没有:

if ( isset($_SESSION['loginStatus']))
{
    // we get here iff the 'loginStatus' session variable has already been
    // created for this user's session.  
    // check if there is a logged-in user, then make visible the 
    // 'User Dashboard' button-link on the Nav bar
    if($_SESSION['loggedInUserName'] != "")
    {
        echo '<script type="text/javascript"> 
             document.getElementById(
                  "UserAccountNavbarLinkId").style.display="block";</script>'; 
        echo '<script type="text/javascript"> 
             document.getElementById(
                  "UserAccountNavbarLinkId").div.className
                            ="pageTopRowContainerLabel";</script>';
    }
    else
    {
        // no logged-in user, so hide the 'User Dashboard' button-link on the Nav bar
        echo '<script type="text/javascript"> 
             document.getElementById(
                       "UserAccountNavbarLinkId").style.display="none";</script>';    
    }
}

Why does my CSS style not get 'honored' when I re-set the div to display=block? 当我将div重新设置为display = block时,为什么我的CSS样式不会被“尊重”?

I tried the following and it did NOT help: in the code above I added the 我尝试了以下内容并没有帮助:在上面的代码中我添加了

document.getElementById(
                  "UserAccountNavbarLinkId").div.className
                            ="pageTopRowContainerLabel

to try to FORCE the div to take up its CSS style again. 尝试强制div再次采用它的CSS样式。 NO change. 没变。

What do I need to do so that when my div appears it goes right back to where it should be -- to the right of the other 4 divs on the Nav bar? 我需要做什么,以便当我的div出现时它会回到应该的位置 - 导航栏上其他4个div的右侧?

Because you're setting it to display=block. 因为您将其设置为display = block。 If you want to revert to whatever the CSS says, use element.style.display = ""; 如果你想恢复到CSS所说的内容,请使用element.style.display = ""; - setting the style to an empty string allows the CSS to take over again. - 将样式设置为空字符串允许CSS再次接管。

You started with display: inline-block , not display: block . 你开始使用display: inline-block ,而不是display: block The div appears below, and full width because, well, you set it to block which does exactly that. div显示在下方,全宽,因为,你将它设置为block ,这就是完全相同的。 Instead, set it back to inline-block . 而是将其设置回inline-block

echo '<script type="text/javascript"> 
         document.getElementById(
              "UserAccountNavbarLinkId").style.display="inline-block";</script>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM