2014-09-29 36 views
0

我發現這script(更多/更少的文本)。當我用jQuery lib 1.11編寫代碼時,他不工作。我不知道爲什麼?jQuery最新的庫不工作腳本

代碼:

<span class="teaser">text goes here</span> 
<span class="complete"> this is the complete text being shown</span> 
<span class="more">more...</span> 

的jQuery:

$(".more").toggle(function(){ 
    $(this).text("less..").siblings(".complete").show();  
}, function(){ 
    $(this).text("more..").siblings(".complete").hide();  
}); 

可以尋求幫助?謝謝

+1

瀏覽器的JavaScript控制檯中是否有錯誤?什麼「不起作用?」意思是說,具體?哦,並且['toggle()'](http://api.jquery.com/toggle-event/)在jQuery 1.9中被刪除;所以*應該*是錯誤。 – 2014-09-29 11:52:58

+0

請參閱http://stackoverflow.com/a/14383246/128165瞭解不推薦使用的'toggle'方法的實現。 – 2014-09-29 12:14:41

回答

2

toggle用於要顯示和隱藏的元素,而不是發生的事件。你可能要綁定到the click eventmore的:

$(".more").click(function(){ 
    if ($(this).siblings(".complete").is(":visible")) 
     $(this).text("more..").siblings(".complete").hide();  
    else 
     $(this).text("less..").siblings(".complete").show();  
}); 

toggle event被棄用的jQuery 1.8和1.9中刪除。

Updated fiddle

+0

請注意,您的小提琴在引用jQuery 1.5.2時起作用。 – 2014-09-29 11:57:15