2011-04-18 82 views

回答

0

与页面的源代码我发现,处理悬停事件这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 
}); 

如果您希望subnav栏保持可见点击或某事之后刚刚创建,检查是否有什么东西被点击了“悬停出”功能在if()语句,即

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

而且你subnav。点击内()功能添加subnavclick变量的设置,如下所示:

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

这应该为你工作....

0

尝试改变

$("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 
    }); 

$("ul#topnav li").click(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 
} 
0

而是在既定做的一个关于鼠标悬停事件的影响现在脚本,你应该使它在the click event上工作。

0

大多数js代码都是多余的,因为无论如何,这些功能都是由css提供的。只需添加一个模拟li的悬浮伪类的“单击”类,然后单击添加并删除它。

http://jsfiddle.net/VirusZ/THYsK/

也让子菜单上悬停甚至出现可见一个点击的元素,你必须更新z折射率。

+0

它工作,只要我不加载一个新的页面,所有的子导航菜单中的链接正在调用一个新页面。那么,如何在加载新页面时保持活动状态?也许我并不清楚自己想要什么。 – n3tx 2011-04-18 18:49:39

+0

@ n3tx在这种情况下,你必须做@ Dutchie432建议的:ajax检索。这是相当复杂的,如果你还不知道,你将不得不学习一些JavaScript。我个人使用jQuery使生活更轻松:http:// api。jquery.com/jQuery.get/。另外我建议读这个:https://github.com/balupton/history.js/wiki/Intelligent-State-Handling。这是一个伟大的库,用于在不刷新页面的情况下允许状态处理。 – 2011-04-19 07:24:27

+0

@ n3tx你也可以看看这个小提琴:http://jsfiddle.net/VirusZ/THYsK/4/的一个简单的例子,监听点击锚和异步检索/替换内容。 – 2011-04-19 07:42:39

0

总而言之,您需要在CSS文件和JS中执行此操作。您需要找到ul#topnav li:hover{...}并将其克隆到ul#topnav li.active{...}之类的东西。这是我们将应用于当前所选菜单项的课程。

ul#topnav li.current{ 
    background: #f00 url(topnav_current.gif) repeat-x; 
} 

从那里,它将取决于您的网站将如何工作。每个标签会带你到一个新的页面,或将点击一个标签触发ajax检索?

如果您使用AJAX,您可以捕捉LI的点击事件来设置类。 (LIVE DEMO)。如果你打算每次点击一个新页面,你需要让JS解析URL并设置正确的项目类使用$(item).addClass('current');