简体   繁体   中英

How to make a text in font/weight style bold in JavaScript

I need change the font weight for a text element in 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.

You need to use:

btn.style.fontWeight = 'bold';

as it's a property of the element itself.

See: http://jsfiddle.net/6ypS8/

您应该直接在按钮上应用您的样式而不是按钮.innerHTML:

btn.style.fontWeight = 'bold';

保持'return false'以防止回发。

 <button id="btn" onclick="this.style.fontWeight = 'bold';return false;" >TEXT</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM