所有按钮上的停止按钮文本更改

Stop Button Text Changing on All Buttons

$('.entry-content #toggle').click(function() {

var elem = $('#toggle').text();

if (elem == 'Read More') {

$('.entry #toggle').text('Read Less');

} else {

$('.entry #toggle').text('Read More'); 

}

});  

此 jQuery 更改存档页面上每篇文章的所有按钮上的按钮文本。我只希望它改变被点击的按钮。

<div id="toggle" class="btn">Read More</div>

<div class="text" />

更新 1:如果有 2 个或更多段落,我只想添加按钮。我想我可以使用 .after 一些方法。

更新 2:

$('#toggle').click(function() {

$(this).closest('.post').find('.text').slideToggle();

$(this).text(function( i, v ) {

   return v === 'Read More' ? 'Read Less' : 'Read More'


      });
});

在事件中引用您自己的按钮。

$('.entry-content #toggle').click(function() {

var elem = $(this).text();

if (elem == 'Read More') {

$(this).text('Read Less');

} else {

$(this).text('Read More'); 

}

});  

参考。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

$('#toggle').click(function() {

    var elem = $(this).text();

    if (elem == 'Read More') {
        $(this).text('Read Less');
    } else {
        $(this).text('Read More'); 
    }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="toggle" class="btn">Read More</div>

<div class="text" />

简化代码。

$('#toggle').click(function() {
    $(this).text(function(i, v){
       return v === 'Read More' ? 'Read Less' : 'Read More'
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="toggle" class="btn">Read More</div>

<div class="text" />