[英]Jquery: changing text inside a button when toggle 'show'
I've created a toggle button to show and hide a div, but I'd like the text inside the button to change from 'Show Content' to "Hide Content' as you toggle 'show' on the #content div. This is a little over my head as I'm still learning jQuery. Any help would be much appreciated! 我创建了一个切换按钮来显示和隐藏div,但是我希望在#content div上切换“显示”时,按钮内的文本从“显示内容”变为“隐藏内容”。我仍在学习jQuery,这让我有点烦恼,不胜感激!
EDIT : I've discovered since I have many posts on a single page, and the code is repeated in all posts, when I click the button to show content on one post it shows the content on all the posts. 编辑 :我发现,因为我在一个页面上有很多帖子,并且在所有帖子中都重复了该代码,当我单击按钮以在一个帖子上显示内容时,它会在所有帖子上显示内容。 How can this be remedied?
如何补救?
HTML HTML
<div class="post-content">
<button id='content-button'>Show Content</button>
<div id='content'>Hello World</div>
</div>
CSS #content { display:none; CSS #content {display:none; }
}
JQUERY JQUERY
jQuery(document).ready(function(){
jQuery('#content-button').live('click', function(event) {
jQuery('#content').toggle('show');
});
});
You can do like this, 你可以这样
jQuery(document).ready(function() {
jQuery('#content-button').live('click', function(event) {
$('#content').is(":visible") ? $(this).text("Show Content") : $(this).text("Hide Content");
jQuery('#content').toggle();
});
});
Check the visibility of the div using is(":visible")
, then change the text 使用
is(":visible")
检查div的可见性,然后更改文本
Edit 编辑
jQuery('#content-button').live('click', function(event) {
$(this).next().is(":visible") ? $(this).text("Show Content") : $(this).text("Hide Content");
$(this).next().toggle();
});
You can use a variable to keep when button is clicked and change text: 您可以使用变量来保留单击按钮时的时间并更改文本:
jQuery(document).ready(function () {
var i = 0;
jQuery('#content-button').live('click', function (event) {
jQuery('#content').toggle('show');
if (i == 0) {
$(this).text("Hide Content");
i = 1;
} else {
$(this).text("Show Content");
i = 0;
}
});
});
I would simply code (sorry for the slide effect, but perhaps you are interested in this too): 我会简单地编写代码(对幻灯片效果感到抱歉,但也许您对此也有兴趣):
JQuery('#content-button').live('click', function(event) {
JQuery("#content").slideToggle("slow",function(){
if (JQuery("#content").css("display")=="none"){
JQuery(this).text("Show Content");
} else {
JQuery(this).text("Hide Content");
}
});
});
jQuery(document).ready(function(){
jQuery('#content-button').live('click', function(event) {
if(jQuery('#content').is(":hidden")){
jQuery(this).text("Hide Content");
jQuery('#content').show();
}
else{
jQuery(this).text("Show Content");
jQuery('#content').hide();
}
});
});
Use .text
to change text of button 使用
.text
更改按钮的文本
jQuery(document).ready(function(){
jQuery('#content-button').live('click', function(event) {
jQuery('#content').toggle('show');
////////////////////////////code to change text
if($('#content-button').text()=="Show Content")
{
$('#content-button').text('hide content');
}
else
{
$('#content-button').text('show content');
}
////////////////////////////
});});
http://jsfiddle.net/syctdh46/11/ http://jsfiddle.net/syctdh46/11/
Most of the answers are similar here. 大多数答案在这里是相似的。
Why not innovate and add a custom function here. 为什么不创新并在此处添加自定义功能。
$.fn.toggleText = function(t1, t2){
if (this.text() == t1) this.text(t2);
else this.text(t1);
return this;
};
And then just edit you code to use the function. 然后只需编辑您的代码即可使用该功能。 So you can use this function anywhere else in your code.
因此,您可以在代码的其他任何地方使用此功能。 Simplifies thing.
简化事情。
jQuery(document).ready(function(){
jQuery('#content-button').live('click', function(event) {
$(event.target).toggleText('Show Content', 'Hide Content'); //Add this line
jQuery('#content').toggle('show');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.