简体   繁体   中英

jQuery change font-family with a quoted variable

I'm trying to apply this CSS with jQuery:

font-family: 'VARIABLE FONT', sans-serif;

Variable is named font , it needs to be in quotes because some fonts have spaces, and I also need to set a fallback sans-serif font.

I've been at this for a long time now, so hopefully some one here can help!

For example the following doesn't work:

$("#something").css('font-family', '"' + font + '", sans-serif');

Use template literals:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

$("#something").css('font-family', `"${font}", sans-serif`);

I think this is how you can achieve what you are trying to do.

$font = $(".fontFamily").text();
$("#something").css("font-family",$font);

I think you can use:

$("#something").css({"font-family": '"'+ font + '"' + ", sans-serif"});

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