如何創建一個<style> tag with Javascript? - How to create a <style> tag with Javascript?

問題:

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>標籤的方法

  1. Is nicer更好

  2. Works with Chrome?適用於 Chrome?

Or maybe或者可能

  1. This is a non-standard thing I should avoid這是我應該避免的非標準事情

  2. Three working browsers are great and who uses Chrome anyway?三個可用的瀏覽器都很棒,但誰還在使用 Chrome?


解決方案:

參考一: https://stackoom.com/question/2CUq
參考二: How to create a
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章