简体   繁体   English

通过JavaScript设置其他样式效果很好,但是如何设置字体系列?

[英]Setting other styles via JavaScript works fine, but how can I set font-family?

For example, on this page , opening the JS console in DevTools and running: 例如,在此页面上 ,在DevTools中打开JS控制台并运行:

document.body.style.color = 'purple'

Sets the color to purple. color设置为紫色。 The style is obviously applied and is visible in DevTools as element.style . 该样式显然已应用,并且在DevTools中作为element.style可见。 But running: 但是运行:

document.body.style.fontFamily = 'Georgia,Cambria,Times New Roman,Times,serif'

Update: even removing the typo (extra semi colon) has no effect. 更新:即使删除错字(多余的半冒号)也无效。

Update: scratch that. 更新:从头开始。 I made a mistake. 我犯了一个错误。 Please delete the question. 请删除问题。 I've already deleted my incorrect answer 我已经删除了我的错误答案

Doesn't work. 不起作用 The style is not applied and is not visible in DevTools under element-style . 样式未应用,在DevTools中的element-style下不可见。

Why isn't setting fontFamily working? 为什么设置fontFamily不起作用? How can I change the font using JavaScript? 如何使用JavaScript更改字体?

Using semicolon inside quotes makes conflict.So Remove semicolon ; 在引号中使用semicolon会引起冲突。因此删除semicolon ;

document.body.style.fontFamily = 'Georgia,Cambria,Times New Roman,Times,serif';

Example: 例:

 var p = document.querySelector('p'); p.style.fontFamily = 'Georgia,Cambria,Times New Roman,Times,serif'; 
 <p>Test Font Familt</p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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