2013-01-10 38 views
0

以下jQuery代碼有什麼問題?jQuery切換和動畫不起作用

<script type="text/javascript"> 
    $("#contact_min").click(function(){ 
    $("#contact_min").toggle(function(){ 
     $("#contact_min").animate({ 
     height: "300px" 
     }, 1500); 
    $(".arrow").html("&#x25bc;")} 
    function(){ 
     $("#contact_min").animate({height: "28px"}, 1500); 
     $(".arrow").html("&#x25B2;") 
    })}); 
</script> 

當我點擊contact_min div時,它什麼都不做。

HTML:

<div id="contact_min"> 
    <span class="arrow">&#x25B2;</span> 
    <span class="text">foobar</span> 
</div> 
+0

請發表您的HTML – Swarne27

+0

嘿,我建議你看看[jQuery的文檔(http://docs.jquery.com/),一切都在說明那裏。 –

+1

不需要那 – Vogel612

回答

5

它不會做任何事。它綁定了另一個事件來處理點擊。當你再次點擊它時,你會看到一些事情發生,但它會綁定另一個事件。之後,每次點擊都會有多個處理程序執行相反的操作,每次點擊都會綁定更多的處理程序。

只需卸下click方法調用,toggle方法結合的點擊事件:

<script type="text/javascript"> 
$("#contact_min").toggle(function(){ 
    $("#contact_min").animate({ 
    height: "300px" 
    }, 1500); 
    $(".arrow").html("&#x25bc;") 
}, 
function(){ 
    $("#contact_min").animate({ 
    height: "28px" 
    }, 1500); 
    $(".arrow").html("&#x25B2;") 
}); 
</script> 
+0

不存在逗號之間缺失的逗號這兩個功能呢? – Aioros

+0

謝謝,我沒有點擊已經自己嘗試過,但添加逗號做了把戲:) – AgeDeO

+0

@Aioros:是的,你說得對,應該有一個逗號。我將它添加到代碼中。 – Guffa

1

沒有.toggle有生我認爲你需要做的是這樣的:

$("#contact_min").click(function(){ 
    var height = $("#contact_min").attr("height"); 
    if height!=28px 
    { 
    $("#contact_min").animate({ 
    height: "28px" 
    }, 1500); 
    $(".arrow").html("&#x25B2;"); 
    } 
    else 
    { 
    $("#contact_min").animate({ 
    height: "300px" 
    }, 1500); 
    $(".arrow").html("&#x25bc;") 
}); 
0

刪除點擊功能,因爲切換已經有交換點擊事件。

因此,這將是:

<script type="text/javascript"> 
$("#contact_min").toggle(function(){ 
$("#contact_min").animate({ 
height: "300px" 
}, 1500); 
$(".arrow").html("&#x25bc;") 
} 
function(){ 
$("#contact_min").animate({ 
height: "28px" 
}, 1500); 
$(".arrow").html("&#x25B2;") 
}); 
</script>