javascript - 如何隐藏来自 CMS 页面的元素

标签 javascript jquery html

我有一个功能可以将选项卡从 CMS 页面加载到我的网页。 如果用户未登录,我试图隐藏选项卡,如果用户登录,则选项卡必须显示。这是填充我的选项卡的代码的一部分。

menu.Append("<li class=' " + page.MenuClass + "'><a dataid='" + page.ID +"' title='" + new HtmlString(page.LongTitle) + "' href='" + href + "' " + IDAttribute + redirectAttribute + ">" + page.ShortTitle + "</a>" + descendants + "</li>");

所以上面获取我的选项卡并将其附加到 menu 我想要做的是,当标题等于 page.longTitle“Account” 时,它必须隐藏此选项卡。

这是我检查用户是否登录的函数

function Check() {
    JApi.Services.CheckLoginAccount(function(account) { // so its using an api to check if the user is logged in
        if (account == null) {
            // if the user is not logged in then the title='Account' must be hidden
        }
    });
}    

有什么办法可以实现这个目标吗?我测试了以上功能是否有效,因此我在页面上隐藏了一个文本框 例如

function Check() {
    JApi.Services.CheckLoginAccount(function(account) {
        if (account == null) {
            $("#txtBox").hide(); //so this gets hidden if the user is not logged in,im just not sure how to get the title "Account" from menu and hide that
        }
    });
}

帐户的输出

<li class=""><a dataid="24770" title=" Account" href="/page?pageId=24770&amp;title=-Account" class="sf-with-ul"> Account<span class="sf-sub-indicator"> »</span></a><ul style="white-space: nowrap; float: none; width: 17em; display: none; visibility: hidden;"><li class=" " style="white-space: normal; float: left; width: 100%;"><a dataid="24771" bannerbase="24770" title="Register" href="/page?pageId=24771&amp;title=Register" style="float: none; width: auto;">Register</a></li></ul></li>

已尝试

$('#menu a[dataid="24770"]').closest('ul').hide(); this way hides it using the dataid,which i dont want.


$('#menu a[title="Account"]').closest('ul').hide(); // if i do it this way it doesnt hide

基于以下答案的尝试 2

$(document).find('#menu li a[title = "Account"]').parent().hide(); //not hiding
$(document).find(" #menu [title='Account']").parent("li").remove();//not hiding

最佳答案

尝试像这样找到元素:

$(document).find('li a[title = "Account"]').parent().hide()

您的元素是动态创建的,因此您需要使用 $(document).find()

编辑

正如 @LouysPatriceBessette 所说,您需要使用 .parent()同样,隐藏整个元素,而不仅仅是 <a>标签

关于javascript - 如何隐藏来自 CMS 页面的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48078292/

相关文章:

javascript - 我怎样才能使滚动锁定?

javascript - 单击图像按钮时无法将 HTML 代码加载到 div 中

javascript - 使用 Jquery 添加没有值的 HTML 属性

javascript - 我可以在 div 调整大小上放置动画/效果吗?

javascript - Myscript 在我的本地主机上不工作,但如果在 jsfiddle 中就没问题

jquery - 如何隐藏 nivo slider 标题区域并使幻灯片可点击

html - 如何解决 Joomla 站点在 Chrome/IE 和 Firefox 中的数字位置问题

javascript - 无法捕获fabricjs中的拖动事件

javascript - 如何创建一个 Observable<any[]>?

javascript - 屏幕上的物体太多时发生碰撞问题