如何在 JavaScript 中使字体/粗细样式的文本加粗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12673615/
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 make a text in font/weight style bold in JavaScript
提问by GibboK
I need change the font weight for a text element in JavaScript:
我需要在 JavaScript 中更改文本元素的字体粗细:
My code here does not work:
我的代码在这里不起作用:
var btn = document.getElementById('accessibilityButton');
btn.innerHTML = 'Default Text';
btn.innerHTML.style.fontWeight = 'bold';
What am I doing wrong?
我究竟做错了什么?
Please note that I don't want to use any libraries (jQuery et. al.) and am looking for a plain JS solution.
请注意,我不想使用任何库(jQuery 等)并且正在寻找一个简单的 JS 解决方案。
回答by m90
You need to use:
您需要使用:
btn.style.fontWeight = 'bold';
as it's a property of the element itself.
因为它是元素本身的属性。
回答by Pierre
You should apply your style directly on your button not on button.innerHTML :
您应该将样式直接应用于按钮而不是 button.innerHTML :
btn.style.fontWeight = 'bold';
回答by Pierre
Keep 'return false' to prevent postback.
保持 'return false' 以防止回发。
<button id="btn" onclick="this.style.fontWeight = 'bold';return false;" >TEXT</button>