简体   繁体   English

webforms:如果项被隐藏,如何删除DOM元素

[英]webforms: how to remove DOM element if item is hidden

I have a aproblem with a horizontal navigation I am using in a small asp.net project. 我在一个小的asp.net项目中使用的水平导航存在一个问题。 The nav contains a Login, Profile and Logout anchor, which are displayed if you are logged in, or not. 导航包含登录,配置文件和注销锚点,如果您已登录,则将显示这些锚点。

I am realizing this behaviour by setting .Visible attribute in Code Behind. 我通过在“隐藏代码”中设置.Visible属性来实现此行为。

Now i want to add a pipe after every element as a "divider". 现在,我想在每个元素之后添加一个管道作为“分隔线”。 This must not be a part of the actual list item itself because it would screw over the a:hover effect. 这一定不能成为实际列表项的一部分,因为它会影响a:hover效果。

But even if the element is not in the DOM tree, die pipe-divider is shown. 但是,即使元素不在 DOM树中,也会显示管道分隔符。 which looks like 看起来像

Login | 登录| | | | |

I have tried solving it with 我试图用解决

if ($("li.nav-item").length == 0) {
            $('span.divider').remove();
        }
        if ($("li.nav-item").length > 0 && $("li.nav-item").is(':visible')) {
            $('<span class="divider"> | </span>').appendTo('li.nav-item');
        }

but this doesnt work. 但这不起作用。 How can I solve this? 我该如何解决?

Kind regards. 亲切的问候。

/edit: html markup (edited IDs) / edit:html标记(已编辑的ID)

<ul id="navigation">
                <li class="nav-item"><asp:HyperLink ID="link1" runat ="server" Text="LoremIpsum"      NavigateUrl="#" /></li>
                <li class="nav-item"><asp:LinkButton ID="LoremIpsum" runat ="server" Text="LoremIpsum" PostBackUrl="~/#.aspx"  /></li>
                <li class="nav-item"><asp:HyperLink ID="link2" runat="server"  Text="LoremIpsum" NavigateUrl="#.aspx"/></li>
                <li class="nav-item"><asp:HyperLink ID="linkLogin" runat="server" Text="Login" NavigateUrl="~/login.aspx"/></li>
                <li class="nav-item"><asp:HyperLink ID="linkProfile" runat="server" Text="Profile" NavigateUrl="~/profile.aspx" Visible="false"/></li>
                <li class="nav-item"><asp:HyperLink ID="linkLogout" runat="server" Text="Logout" NavigateUrl="~/logout.aspx" Visible="false"/></li>
                <li><asp:HyperLink ID="LoremIpsum" runat="server" Text="LoremIpsum" NavigateUrl="~/nutzungsbedingungen.aspx"/></li>
                </ul>


if (Session["svar_loggedin"] != null)
    {
        linkLogin.Visible = false;
        linkProfile.Visible = true;
        linkLogout.Visible = true;
    }
    else
    {
        linkLogin.Visible = true;
        linkProfile.Visible = false;
        linkLogout.Visible = false;
    }

Try this js instead: 试试这个js代替:

$.each($("li.nav-item").children("a").filter(":visible"), function (e) {
    $('<span class="divider"> | </span>').appendTo($(this));
});

EDIT: 编辑:

Either use css instead of Visible property: 使用css代替Visible属性:

linkLogin.Attributes.Add("style", "display:none")

or alt. 或alt。 iterate through the controls, check if visible == true, if so add the span-markup to the Text-attribute 遍历控件,检查是否可见== true,如果是,则将span-markup添加到Text-attribute

  $(function () {
        var elements = $('li.nav-item > a').filter(':visible');
        for (var i = 0; i < elements.length - 1; i++) {
            elements.eq(i).append($('<span class="divider"> | </span>'));
        }

    });

in my case this did the trick. 就我而言,这是成功的秘诀。 I was not realising that my Code Behind was altering the asp:Hyperlink controls and not the list items. 我没有意识到我的背后代码正在更改asp:Hyperlink控件,而不是列表项。

I changed my Jquery Snippet accordingly to relate to all anchors which are a child of list-items with the class 'nav-item'. 我相应地更改了我的Jquery代码段,使其与所有锚(它们是“ nav-item”类的列表项的子项)相关。

thank you all for bringing me back on track! 谢谢大家让我重回正轨!

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

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