2009-08-27 15 views
7

मैं मेनू बनाना चाहता हूं, और क्लिक करते समय कक्षा बदलना चाहता हूं।लिंक पर क्लिक करते समय सक्रिय ली बदलें jquery

जब मैं class="active" के साथ "ली" पर क्लिक करता हूं, तो मुझे खाली <li> पर कक्षा जोड़ने और jtery "li" से निकालने के लिए jquery चाहिए।

<li class="active"><a href="javascript:;" onclick="$.data.load(1);">data</a></li> 
<li><a href="javascript:;" onclick="$.data.load(2);">data 2</a></li> 

क्या कोई मेरी मदद कर सकता है? :)

उत्तर

19

मुझे लगता है कि आप इस मतलब है:

$('li > a').click(function() { 
    $('li').removeClass(); 
    $(this).parent().addClass('active'); 
}); 
+0

thx alot के लिए एक सक्रिय कक्षा रखता है! यह सही काम करता है .. – william

12
// When we click on the LI 
$("li").click(function(){ 
    // If this isn't already active 
    if (!$(this).hasClass("active")) { 
    // Remove the class from anything that is active 
    $("li.active").removeClass("active"); 
    // And make this active 
    $(this).addClass("active"); 
    } 
}); 
0

यह आपको बंद कर देना चाहिए।

$("li").click(function() { 
    $("li").removeClass("active"); 
    $(this).addClass("active"); 
}); 
2
$('li').click(function() 
{ 
    $('li', $(this).parent()).removeClass('active'); 
    $(this).addClass('active'); 
} 
2
$(window).load(function(){ 
    page=window.location.pathname.split("/").pop(); 
    menuChildren = $('a[href="' + page + '"]'); 
    $(menuChildren).parent('li').addClass('active'); 
}); 

ऊपर कोड यूआरएल को देखने और पिछले तत्व पॉप (जो फ़ाइल का नाम है) होगा। फिर यह href विशेषता के साथ एंकर टैग पाता है जिसमें यूआरएल का वही मान होता है, फिर यह अपने माता-पिता li टैग

संबंधित मुद्दे