Css jquerytoggel类
我有一个手风琴菜单,它是使用下面的脚本开发的Css jquerytoggel类,css,jquery-ui,accordion,addclass,Css,Jquery Ui,Accordion,Addclass,我有一个手风琴菜单,它是使用下面的脚本开发的 $(document).ready(function(){ $('li.button a').click(function(e){ $(this).addClass('active'); /* Finding the drop down list that corresponds to the current section: */ var dropDown = $(this).parent().
$(document).ready(function(){
$('li.button a').click(function(e){
$(this).addClass('active');
/* Finding the drop down list that corresponds to the current section: */
var dropDown = $(this).parent().next();
/* Closing all other drop down sections, except the current one */
$('.dropdown').not(dropDown).slideUp('100');
dropDown.slideToggle('100');
/* Preventing the default event (which would be to navigate the browser to the link's address) */
e.preventDefault(); })
});
我需要在单击菜单时添加.active类,因此我在jquery中添加了addclass,它正在添加该类,但问题是在单击另一个菜单时应该删除该类。我甚至还试着换了一节课,但都没用
此外,我还希望在打开页面时默认打开第一个li项。只需在函数开头添加以下内容:
$('li.button a').click(function(e){
$('li.button a').removeClass('active');
$(this).addClass('active');
...
});
试试这个
$('li.button a').each(function(){
$(this).removeClass("active").addClass("no-active"); // your non active class
});
嘿,谢谢。还有一件事,在加载页面时,默认情况下如何打开第一个列表项?$(function(){$('li.button a:first').addClass('active');})
$('li.button a').click(function(e){
if($(this).hasClass('active')) // this line verifies if the actual clicked element is already active
return false; // you can return false or do another operation here
$('li.button a.active').removeClass('active');
$(this).addClass('active');
/* Finding the drop down list that corresponds to the current section: */
var dropDown = $(this).parent().next();
/* Closing all other drop down sections, except the current one */
$('.dropdown').not(dropDown).slideUp('100');
dropDown.slideToggle('100');
/* Preventing the default event (which would be to navigate the browser to the link's address) */
e.preventDefault();
})