2012-12-17 42 views
0

我有以下幾點:更改切換的錨標記內的文本?

enter image description here

所以現在我切換ul.children這樣的:

$('a.expand-cat-item').on('click', function() { 
    $(this).closest('li.cat-item').find('ul.children').toggle('slow'); 
}); 

現在,我也會改變的+號爲 - 號內a.expand-cat-item(這也將作爲一個切換)。

如何做到這一點?

回答

4

您可以使用條件運算符? :使文本開關狀態。

$('a.expand-cat-item').on('click', function() { 
    $(this).closest('li.cat-item').find('ul.children').toggle('slow'); 
    $(this).text($(this).text() == "+" ? "-" : "+"); 
}); 
+0

是文字隱含的內容或不'text'需要設置的地方? –

+0

更新了我的答案,謝謝@JosephMarikle – Adil

0

使用此:

$('a.expand-cat-item').text("- "); 
1

嘗試

$('a.expand-cat-item').on('click', function() { 
    $(this).closest('li.cat-item').find('ul.children').toggle('slow'); 
    if ($(this).text() == '+ '){ 
     $(this).text('- '); 
    } 
    else{ 
     $(this).text('+ '); 
    } 
}); 
0
$('a.expand-cat-item').on('click', function() { 
    var $this = $(this); 
    $this.closest('li.cat-item').find('ul.children').toggle('slow'); 
    var html = '+'; 
    if($this.html().indexOf('+') > -1){ 
     html = '-'; 
    } 
    $this.html(html); 
});