简体   繁体   中英

Change button text to bold on click

I'm trying to make the text of a button become bold when it is clicked on. I have tried using the advice from this question: How to make a text in font/weight style bold in JavaScript

http://jsfiddle.net/14tjwvnq/

function boldButton(){
    $('#btn').style.fontWeight = '700';
}

In jQuery you need to use css method:

$('#btn').css( 'font-weight', '700' );

jsFiddle

YOU MIGHT NOT NEED JQUERY

//Inline
<button class="btn" id="btn1" onclick="this.style.fontWeight = 'bold'">Test1</button>
//OR with function
<button class="btn" id="btn2" onclick="boldButton(this)">Test2</button>

<scrypt>
function boldButton(btn){
    btn.style.fontWeight =  '700';
}
</scrypt>

fiddle

You need to convert jquery object to javascript object before setting the property through javascript. also make sure that onclick method is defined :

 function boldButton(num){
   $('#btn'+num)[0].style.fontWeight = '700';
 }

Working Demo

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