如何使用 jQuery 更改 Bootstrap 3 按钮的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23978864/
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
How to change the text of a Bootstrap 3 button with jQuery?
提问by Ignatius Samuel Megis
How to retrieve button text inside tag button?
如何检索标签按钮内的按钮文本?
Example HTML:
示例 HTML:
<button type="button" class="btn btn-danger" id="dislikes" value="notThisValue">DISLIKES<span class="glyphicon glyphicon-thumbs-down"></span></button>
My jQuery:
我的jQuery:
$(document).ready(function(){
$('#dislikes').click(function() {
$("#dislikes").val('Hello World');
})
});
I need to change the text DISLIKESto YOU DISLIKE IT. My jQuery has to change the inner text of the button not the value "notThisValue".
我需要将文本DISLIKES更改为YOU DISLIKE IT。我的 jQuery 必须更改按钮的内部文本而不是值“notThisValue”。
回答by Hammad
The reason you are not able to change the text is that you are changing its value using .val()
. Instead you need to use .text()
to change tag's text.
您无法更改文本的原因是您正在使用.val()
. 相反,您需要使用.text()
来更改标签的文本。
Try this.
尝试这个。
$(document).ready(function(){
$('#like').click(function() {
$("#like").text('YOU DISLIKE IT');
})
});