在jquery中单击时更改按钮文本(显示/隐藏/显示) [英] Change button text (show / hide/ show) on click in jquery toggle
本文介绍了在jquery中单击时更改按钮文本(显示/隐藏/显示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究这个小提琴: http://jsfiddle.net/cED6c/7/ 我想在单击时更改按钮文本,并尝试使用以下代码:
I am working on this fiddle: http://jsfiddle.net/cED6c/7/ I want to make the button text change on click and I tried using the following code:
<input type="button" class="reveal" onClick="this.value=this.value=='Show Answer'?'Hide Answer':'Show Answer'" value="Show Answer">
但是,它不起作用.我应该如何实施呢?任何帮助将是巨大的.
However, it does not work. How should I implement this? Any help would be great.
推荐答案
您需要添加以下代码:
if ($.trim($(this).text()) === 'Show Answer') {
$(this).text('Hide Answer');
} else {
$(this).text('Show Answer');
}
您可以在工作中看到它:
You can see it at work:
我在显示答案后的空格中添加了修整函数原因.
I add the trim function cause in the html you got space after the Show Answer.
这篇关于在jquery中单击时更改按钮文本(显示/隐藏/显示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文