在<head>之后包含CSS的正确方法

Ale*_*lex 17 html css html5 w3c

<link rel="stylesheet" ...根据W3C标准,显然添加文档正文被认为是一种不好的做法.<style>在体内添加块也一样......

那么是否有任何符合标准的解决方案可以在<head>标签之外添加CSS ?就像在文档的最后.

Mad*_*iha 26

如果您只想在特定事件中包含CSS样式,那么就没有什么能阻止您这样做:

var linkElement = document.createElement("link");
linkElement.rel = "stylesheet";
linkElement.href = "path/to/file.css"; //Replace here

document.head.appendChild(linkElement);
Run Code Online (Sandbox Code Playgroud)

这具有以异步方式添加样式表的额外好处,这不会阻止浏览器下载任何其他内容.