jQuery:单击时更改按钮文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19365386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: Change button text on click
提问by Scherf
After seeing Toggling button text in jquerythis question, I tried to re create it in my situation but can't seem to have it work.
在 jquery这个问题中看到Toggling button text之后,我尝试在我的情况下重新创建它,但似乎无法让它工作。
Here is a fiddle of what I've done: http://jsfiddle.net/V4u5X/
这是我所做的事情:http: //jsfiddle.net/V4u5X/
$('.SeeMore2').click(function(){
var $this = $(this);
$this.toggleClass('SeeMore');
if($this.hasClass('.SeeMore2')){
$this.text('See More');
} else {
$this.text('See Less');
}
});
It seems to only ever run the if statement once. What am I missing?
它似乎只运行一次 if 语句。我错过了什么?
回答by Kierchon
$('.SeeMore2').click(function(){
var $this = $(this);
$this.toggleClass('SeeMore2');
if($this.hasClass('SeeMore2')){
$this.text('See More');
} else {
$this.text('See Less');
}
});
This should do it. You have to make sure you toggle the correct class and take out the "." from the hasClass
这应该这样做。您必须确保切换正确的类并去掉“。” 从 hasClass
回答by Abhishek
try this, this javascript code to change text all time to click button.http://jsfiddle.net/V4u5X/2/
试试这个,这个 javascript 代码总是将文本更改为单击按钮。http://jsfiddle.net/V4u5X/2/
html code
html代码
<button class="SeeMore2">See More</button>
javascript
javascript
$('.SeeMore2').click(function(){
var $this = $(this);
$this.toggleClass('SeeMore2');
if($this.hasClass('SeeMore2')){
$this.text('See More');
} else {
$this.text('See Less');
}
});
回答by Naresh Kumar
its work short code
它的工作短代码
$('.SeeMore2').click(function(){
var $this = $(this).toggleClass('SeeMore2');
if($(this).hasClass('SeeMore2'))
{
$(this).text('See More');
} else {
$(this).text('See Less');
}
});
$('.SeeMore2').click(function(){
var $this = $(this).toggleClass('SeeMore2');
if($(this).hasClass('SeeMore2'))
{
$(this).text('See More');
} else {
$(this).text('See Less');
}
});
回答by mlnyc
This should work for you:
这应该适合你:
$('.SeeMore2').click(function(){
var $this = $(this);
$this.toggleClass('SeeMore2');
if($this.hasClass('SeeMore2')){
$this.text('See More');
} else {
$this.text('See Less');
}
});
回答by Wajid khan
In HTML:
在 HTML 中:
<button type="button" id="AddButton" onclick="AddButtonClick()" class="btn btn-success btn-block ">Add</button>
In Jquery write this function:
在 Jquery 中编写这个函数:
function AddButtonClick(){
//change text from add to Update
$("#AddButton").text('Update');
}