Jquery 菜单包括:悬停、单击、类更改和ajax
最后一个最小化的代码是,我希望它能帮助某些人:Jquery 菜单包括:悬停、单击、类更改和ajax,jquery,ajax,menu,click,hover,Jquery,Ajax,Menu,Click,Hover,最后一个最小化的代码是,我希望它能帮助某些人: $("#menu").find("a").hover(function(){ $(this).addClass("active"); },function(){ $(this).not(".clicking").not(".selected").removeClass("active"); }); $("#menu").find("a").click(function(){ var $this = $(this);
$("#menu").find("a").hover(function(){
$(this).addClass("active");
},function(){
$(this).not(".clicking").not(".selected").removeClass("active");
});
$("#menu").find("a").click(function(){
var $this = $(this);
$("#ajaxP").fadeIn("fast");
$("#normalcontent").hide("fast").load($this.attr("href") +" #normalcontent","",function(){
$("#normalcontent").slideDown("slow");
});
$("#primarycontainer").hide("fast").load($this.attr("href") +" #primarycontainer","",function(){
$("#primarycontainer").slideDown("slow");
$("#ajaxP").fadeOut("fast");
})
$this.closest('ul').find('a').removeClass('active clicking selected');
$this.addClass('active clicking selected');
return false;
});
编辑:谢谢你的回答,它现在正在工作。我添加了一个额外的类selectedwhich>在css中没有任何内容,并相应地编写了代码。这是新代码。如何最小化>此代码
这是:
你好!!我制作了一个菜单,将悬停时处于活动状态的类添加到每个li,并在悬停时删除>类,但在已处于活动状态的li上除外。
到目前为止,这项工作已经完成。不过我还有一个问题,用ajax单击每个加载内容到>某处的li。问题从这里开始,当我单击时,我想将class active>添加到单击的元素中,并从所有元素中删除类。我添加了这个类,但是鼠标悬停时,在点击之前激活了>类的li没有激活,我想激活的>类没有从中删除吗?有人能帮忙吗
你应该考虑回到那块代码的绘图板上。 首先,您不需要在包装集上使用.each,因为jQuery已经存在 这是为你做的
$("#menu").find("a").not(".active").hover(...);
很好
要实现我想您想要的,请使用以下代码:
$('#menu').find('a').bind('click', function(){
var $this = $(this);
$this.closest('ul').find('a').removeClass('active');
$this.addClass('active');
});
如果你发布更简洁的问题会更好。如果我理解你的问题,那是因为$menu。。。。函数在页面第一次加载时绑定到每个元素,而当页面加载时blog被设置为活动状态时,它不会将函数绑定到它。试一试
$("#menu").find("a").each(function(){
$(this).hover(function(){
if (!($(this).hasClass("selected")){
alert($(this));
$(this).addClass("active");
}
},function(){
$(this).not(".clicking").removeClass("active");
});
});
您可能需要在上面的第二个函数上使用另一个if,具体取决于您想要的是什么。很抱歉我的问题是共谋的:不是问题,但最好清楚地说明问题是什么:它当前在做什么以及您希望它做什么。同样,你也倾向于通过这种方式获得更多的观点和答案,这对每个人都有好处!
$("#menu").find("a").not(".active").each(function(){
$(this).hover(function(){
alert($(this));
$(this).addClass("active");
},function(){
$(this).not(".clicking").removeClass("active");
});
});
$("#homeLink").click(function(){
var myThis=$(this);
$("#ajaxP").fadeIn("slow");
$("#normalcontent").hide("slow").load("index.php #normalcontent").slideDown("slow");
$("#primarycontainer").hide("slow").load("index.php #primarycontainer").slideDown("slow");
$("#ajaxP").fadeOut("normal");
$("#menu").find("a").each(function(){
$(this).unbind('mouseover').unbind("mouseout");
$(this).removeClass("active clicking");
});
myThis.addClass("active clicking");
return false;
});
$("#menu").find("a").not(".active").hover(...);
$('#menu').find('a').bind('click', function(){
var $this = $(this);
$this.closest('ul').find('a').removeClass('active');
$this.addClass('active');
});
$("#menu").find("a").each(function(){
$(this).hover(function(){
if (!($(this).hasClass("selected")){
alert($(this));
$(this).addClass("active");
}
},function(){
$(this).not(".clicking").removeClass("active");
});
});