[英]jQuery change font-family with a quoted variable
I'm trying to apply this CSS with jQuery:我正在尝试将此 CSS 与 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.变量名为
font
,它需要用引号引起来,因为一些 fonts 有空格,我还需要设置一个备用无衬线字体。
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 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"});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.