2011-05-08 64 views
0

我有一个按钮,上有文字...它的格式,像这样jQuery的 - 所示,当隐藏更改时显示的文本

<a href="" id="refl" class="button"><span>VIEW ALL CASES</span></a> 

我有一些jQuery的那个切换一个div当“REFL”的时刻被点击。

当div被隐藏时,如何将文本查看所有案例改为“查看所有案例”,但当div显示时显示“CLOSE ALL CASES”?

干杯,

回答

0
$('.button').click(function() { 
    var span = $(this).find('span') 
    span.html(span.html() == 'CLOSE ALL CASES' ? 'VIEW ALL CASES' : 'CLOSE ALL CASES'); 
}); 

我选择使用的.html()代替的.text(),因为你可以有你的跨度内的其他HTML标记。

0
$('a#refl').click(function() { 
    //select elements 
    var $span = $('span', this); 
    var $div = $('div#theOneYouAreHidding'); //this is div you hide/show 

    //check text to see if we need to hide or show 
    if($span.text() == 'VIEW ALL CASES') 
    { 
     $div.show(); 
     $span.text('CLOSE ALL CASES'); 
    } 
    else 
    { 
     $div.hide(); 
     $span.text('VIEW ALL CASES'); 
    } 
}); 
0
$('#refl').click(function() { 

    $(this).text(function() { 
     return $('#your-element:visible').length ? 'CLOSE ALL CASES' : 'SHOW ALL CASES'; 
    }); 

    // hide code 

}); 
0
$('a#ref1').toggle(
    function() { 
    $('div').show(); // div selector here 
    $(this).find('span').html('CLOSE ALL CASES'); 
    }, 
    function() { 
    $('div').hide(); // div selector here 
    $(this).find('span').html('VIEW ALL CASES'); 
    }, 
); 
2
$("#ref1").click(function(){ 
    var div = $("#theDivToToggle"); 
    div.toggle(); 
    $(this).find("span").text(div.is(":visible") ? "CLOSE ALL CASES" : "SHOW ALL CASES"); 
}); 
+0

这种做法在一些贴别人的独特优势是,它使用的'#theDivToToggle'实际的知名度,以确定应该显示哪些文本。您可以查看当前文本(例如,如果它显示“关闭所有案例”,然后将其更改为“查看所有案例”),但如果您显示/隐藏它们,可能会使您与实际内容不同步以任何其他方式。 – VoteyDisciple 2011-05-08 12:58:57