我有 onclick
方法,它使文本样式为“粗体”,如果我第二次单击,我需要将其恢复正常。
我怎样才能做到这一点 ?
<button onclick="boldText()">B</button>
function boldText() {
document.getElementById('txt-area').style.fontWeight = 'bold';
}
最佳答案
setBold
函数检查元素 fontWeight
是否为 bold
然后将其设置为 bold
,反之亦然
function setBold() {
var element= document.getElementById('txt-area');
if (element.style.fontWeight == 'bold') {
element.style.fontWeight = 'normal';
} else {
element.style.fontWeight = 'bold';
}
}
<button onclick="setBold()">Toggle font Weight</button>
<p id='txt-area'>Test Text</p>
关于javascript - 如何在 'onclick' 方法之前返回状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53904426/