获取当前 URL 并与链接的 HREF 进行比较以保持子菜单打开

发布于 2024-10-19 13:24:56 字数 365 浏览 3 评论 0原文

我有一个简单的菜单,当单击父 LI 时,如果有子 UL 要跟随,该菜单会向下滑动更多 UL...

我试图做到这一点,以便当菜单位于某个目录中时,它将将该菜单扩展为默认而不是关闭。我尝试了一些方法,但就是没能发挥作用。目前,我有一个 IF 语句,将 PATHNAME 与变量进行比较,并只是告诉 ID 保持 UL 打开......这已经是我能得到的最接近的结果。

如果页面 url 与父 UL HREF 相同,关于如何使 UL 保持打开状态,有什么建议吗?

您可以在此处查看代码: http://jsfiddle.net/RvGMQ/

谢谢!

I have a simple menu that slides down more UL when the parent LI is clicked, if there is a child UL to follow...

I am trying to make it so when the menu is in a certain directory it will have that menu expanded by default rather than closed. I have tried a few things, but am just not getting it to work. Currently I have a IF statement comparing the PATHNAME with the Varibles and just telling that ID to keep the UL open... That has been as close as I can get.

Any suggestions on how to make the UL stay open if the page url is the same as the parent UL HREF?

You can see the code here: http://jsfiddle.net/RvGMQ/

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不是你的备胎 2024-10-26 13:24:56

好的,所以我在这里有事要做。如果您遇到这篇文章,请告诉我是否有更好的方法...

我最终创建了一个名为 MyTitle 的元标记,并为其提供内容来比较 ID,然后根据该元标记显示 UL。

    $(document).ready(function() {
    $("#menu ul ul").hide();
    $("#menu ul li").click(function()
    {
        $(this).next("ul").slideToggle(300).siblings("ul").slideUp("slow");
    });

//created a meta tag called MyTitle to use as a way to open menu when needed. This function checks for the value of MyTitle. Got this from (http://forums.macrumors.com/showthread.php?t=569947)
var mytitle;
var metas = document.getElementsByTagName('meta');
for (var x=0,y=metas.length; x<y; x++) {
  if (metas[x].name.toLowerCase() == "mytitle") {
    mytitle = metas[x];
  }
}

//Find the page currently on and display menu if need be.
var title = "" + mytitle.content;
if(title == "Resources")
    $("ul #resources").show();
if(title == "Conferences")
    $("ul #conferences").show();

ETC..........

Ok, so I got something to work here. Let me know if there is a better way if you come across this post...

I ended up creating a Meta Tag called MyTitle and gave it content to compare an ID to then display UL based on that Meta Tag.

    $(document).ready(function() {
    $("#menu ul ul").hide();
    $("#menu ul li").click(function()
    {
        $(this).next("ul").slideToggle(300).siblings("ul").slideUp("slow");
    });

//created a meta tag called MyTitle to use as a way to open menu when needed. This function checks for the value of MyTitle. Got this from (http://forums.macrumors.com/showthread.php?t=569947)
var mytitle;
var metas = document.getElementsByTagName('meta');
for (var x=0,y=metas.length; x<y; x++) {
  if (metas[x].name.toLowerCase() == "mytitle") {
    mytitle = metas[x];
  }
}

//Find the page currently on and display menu if need be.
var title = "" + mytitle.content;
if(title == "Resources")
    $("ul #resources").show();
if(title == "Conferences")
    $("ul #conferences").show();

etc..........

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文