javascript - 我想让顶部选项卡在单击时保持打开状态

标签 javascript jquery css

您好,我正在使用此处找到的菜单: http://www.sohtanaka.com/web-design/examples/horizontal-subnav/

我遇到的问题是我希望在单击时保留顶部导航并同时显示子目录。我希望任何人都可以帮助我!

加载新页面时,顶部导航应该仍处于事件状态。我知道如何使用 JS/jQuery 在服务器端而不是客户端

最佳答案

通过快速查看页面源代码,我找到了这段处理悬停事件的 jQuery 代码:

$("ul#topnav li").hover(function() { //Hover over event on list item
    $(this).css({ 'background' : '#1376c9 url(topnav_active.gif) repeat-x'}); //Add background color + image on hovered list item
    $(this).find("span").show(); //Show the subnav
} , function() { //on hover out...
    $(this).css({ 'background' : 'none'}); //Ditch the background
    $(this).find("span").hide(); //Hide the subnav
});

如果您希望子导航栏在点击后保持可见或其他内容,只需在“悬停”函数中创建一个 if() 语句来检查是否已点击某些内容,即

} , function() { //on hover out...
    $(this).css({ 'background' : 'none'}); //Ditch the background
    if(subnavclick==0){
        $(this).find("span").hide(); //Hide the subnav
    }
});

然后在您的 subnav .click() 函数中添加 subnavclick 变量的设置,如下所示:

 $("span").click(function(){
   subnavclick==1;
   //do other stuff
 });

这应该适合你....

关于javascript - 我想让顶部选项卡在单击时保持打开状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5702845/

相关文章:

javascript - javascript 中带有过滤器的元素计数器

jQuery 加载外部站点页面

javascript - 单击该图像时如何在canvas html5中获取图像ID?

javascript - react native - react-native-maps initialRegion 不起作用

jquery - JavaScript 生成的文本消失(Cufon 和 Clone)

javascript - 从 html 文本中获取数字,但它们显示为正方形

CSS - 内联 block 不是直接并排

jquery - 媒体查询和 jquery ".slideToggle()"在调整大小时更改 li 元素的可见性

javascript - javascript 开发人员在什么情况下使用 map() 方法?

javascript - 输入带有图像变化的广播列表