[英]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.