【问题标题】:Change button text with jQuery [duplicate]使用 jQuery 更改按钮文本 [重复]
【发布时间】:2020-06-15 15:54:15
【问题描述】:
如何在jQuery的帮助下将Text1和Text2更改为AnotherText1、AnotherText2?
<div class="col-md-8">
<div class="btn-group pull-right">
<button type="submit" class="btn btn-primary">Text1</button>
</div>
<div class="btn-group pull-left">
<button type="reset" class="btn btn-warning">Text2</button>
</div>
</div>
【问题讨论】:
标签:
jquery
html
button
text
【解决方案1】:
//if you want to change the text only then use
$("button[type='submit']").text('anything1');
$("button[type='reset']").text('anything2');
//if you want to change the text on click a event then use
$("button[type='submit']").click(function(e) {
$(this).text('on click anything1');
});
$("button[type='reset']").click(function(e) {
$(this).text('on click anything2');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div class="col-md-8">
<div class="btn-group pull-right">
<button id="btn1" type="submit" class="btn btn-primary">Text1</button>
</div>
<div class="btn-group pull-left">
<button type="reset" class="btn btn-warning">Text2</button>
</div>
</div>
【解决方案2】:
请检查一下
<div class="col-md-8">
<div id="btn1" class="btn-group pull-right">
<button type="submit" class="btn btn-primary">Text1</button>
</div>
</div>
<script>$('#btn1').text("AnotherText1");</script>
【解决方案3】:
您可以使用 .html('AnotherText') 实现此目的
例子:
<button type="reset" id="btn1" class="btn btn-warning">Text1</button>
<script> $("#btn1").html('AnotherText1') </script>