2013-01-21 66 views
1

我的div打開一個按鈕,該按鈕有兩個類(活動或不活動),我想改變類,當我點擊該boby和工程,但當我再次點擊在按鈕上,班級被霧化。關閉div並更改按鈕類jquery

我搜索了很多,但我只是發現改變類或關閉身體點擊div,你能幫我把它們兩個結合起來嗎?

請謝謝!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>TEST</title> 
<script type="text/javascript" src="jquery-1.7.1.min.js"></script> 
</head> 
<style> 
body{background:#1E1E1E;} 
.link{background:gray;color:black;} 
.link_active{background:green;color:white;} 
#content{background:gray;border:1px solid;width:100px;height:100px;display:none;} 
</style> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $(".link").toggle(function(){ 
     $(this).toggleClass("link_active"); 
    }, 
    function() { 
     $(this).removeClass("active"); 
    }); 
    $(".link").click(function(){ 
     $(this).next("#content").slideToggle("fast"); 
     $(this).removeClass("active"); 
    }); 
    $(document).on('click', function() { 
     $('#content').css("display","none"); 
     $('.link').removeClass("link_active"); 
     $('.link').addClass("link"); 
    }); 
}); 
</script> 
<body> 
<a href="#" class="link">OPEN</a> 
<div id="content"> content content content content content content</div> 
</body> 
</html> 

回答

2

看到這個:http://jsfiddle.net/LVfdk/2/

$(document).ready(function() { 

$(".link").click(function (e) { 
    $(this).next("#content").slideToggle("fast"); 
    $(this).toggleClass("link_active"); 
    e.stopPropagation(); 
}); 
$(document).on('click', function (e) { 
    if (e.target.id !== "content") { 
     $('#content').css("display", "none"); 
     $('.link').removeClass("link_active"); 
     $('.link').addClass("link"); 
    } 
}); 
}); 
+0

幾乎沒有,當我點擊的內容也消失:( – novoa

+0

@novoa,看到更新的答案... – Anujith

+0

多數民衆贊成在完美,非常感謝你!!!!!!!!!! – novoa

0
$(document).on('click', function() { 
     $('#content').toggle(); 
     $('.link').toggleClass("link_active link"); 

    }); 
+0

我不能讓它工作我的朋友... – novoa