問題:
I'm looking for a way to insert a <style>
tag into an HTML page with JavaScript.我正在尋找一種使用 JavaScript 將<style>
標記插入 HTML 頁面的方法。
The best way I found so far:迄今爲止我發現的最好方法:
var divNode = document.createElement("div");
divNode.innerHTML = "<br><style>h1 { background: red; }</style>";
document.body.appendChild(divNode);
This works in Firefox, Opera and Internet Explorer but not in Google Chrome.這適用於 Firefox、Opera 和 Internet Explorer,但不適用於 Google Chrome。 Also it's a bit ugly with the <br>
in front for IE.對於 IE 來說,前面的<br>
也有點難看。
Does anyone know of a way to create a <style>
tag that有誰知道創建<style>
標籤的方法
Is nicer更好
Works with Chrome?適用於 Chrome?
Or maybe或者可能
This is a non-standard thing I should avoid這是我應該避免的非標準事情
Three working browsers are great and who uses Chrome anyway?三個可用的瀏覽器都很棒,但誰還在使用 Chrome?
解決方案:
參考一: https://stackoom.com/question/2CUq參考二: How to create a