document.querySelector( "style" ).innerHTML +="html {" +" background-color: #0dd;" +"}";html { background-color: #fff; font-family: Arial;}body { display: flex; justify-content: center;}td { background-color: #fafafa;}th:first-child, td:first-child { border-left-style: none;}th:last-child, td:last-child { border-right-style: none;}th:first-child { border-top-left-radius: 1rem;}th:last-child { border-top-right-radius: 1rem;}tr:last-child td { border-bottom-style: none; background-color: #efefef;}tr:last-child td:first-child { border-bottom-left-radius: 1rem;}tr:last-child td:last-child { border-bottom-right-radius: 1rem;}<html> <head> <style> ::selection { background-color: #0df; color: #0df; } table, th, td { padding: 1rem; border-style: solid; border-color: #eee; border-collapse: collapse; } table { margin-top: 1rem; border-style: none; } th { border-top-style: none; border-color: #111; background-color: #222; color: #fff; border-bottom-color: #fff; } </style> </head> <body> <table> <tr> <th>One</th><th>Two</th><th>Three</th> </tr> <tr> <td>four</td> <td>five</td><td>six</td> </tr> <tr> <td>seven</td> <td>eight</td><td>nine</td> </tr> </table> </body></html>document.querySelector( "style" ).innerHTML +="html {" +" background-color: #0dd;" +"}";我有大量的 HTML/CSS 文件內容,我想將它們“注入”到網頁的某些部分,就像我style在本例中使用標簽所做的那樣。我希望保持這些文件不變,如下所示:document.querySelector( "style" ).innerHTML += " html { background-color: #0dd; }";無需將每個 JavaScript 行用引號括起來,并在每行末尾添加 + 運算符。我是否必須將所有代碼轉換為這種格式?或者 JavaScript 中是否有一種方法可以將此 HTML/CSS 代碼按原樣添加到元素中;無需逐行更改原始代碼的格式?與這里具體的 CSS 和 HTML 代碼無關。我只是用它作為例子。
忽略 JavaScript 引用內容中的空格
慕桂英4014372
2023-07-29 14:56:52