我的页面有两个文本区域和一个 div。一个textarea是供用户输入html,另一个是输入css。单击按钮时,我将调用一个 javascript 函数来显示 div 内的 css 和 html。
function handleLaunch() {
var div = document.getElementById('pane');
var html = document.getElementById('h');
var css = document.getElementById('c');
div.innerHTML = html.value;
// This line obviously doesn't work div.style = css.value;
}
<body>
<textarea id="c" placeholder="CSS code here..." rows="10" cols="50"></textarea>
<div id="pane">
</div>
<br>
<textarea id="h" placeholder="Html code here..." rows="10" cols="50"></textarea>
<br>
<button type="button" onclick="handleLaunch()">Launch</button>
现在,如何将 css 设置为文本?
编辑:我应该提到我希望能够放置类似 .classExample{text-align:center} 在 css textarea 中并应用它。
最佳答案
是的,@adeneo 的回答有效,这个片段显示了它。输入 color: red;
例如作为 CSS,以及任何文本作为 HTML...
function handleLaunch() {
var div = document.getElementById('pane');
var html = document.getElementById('h');
var css = document.getElementById('c');
div.innerHTML = html.value;
div.style.cssText = css.value;
}
<body>
<textarea id="c" placeholder="CSS code here..." rows="10" cols="50"></textarea>
<div id="pane">
</div>
<br>
<textarea id="h" placeholder="Html code here..." rows="10" cols="50"></textarea>
<br>
<button type="button" onclick="handleLaunch()">Launch</button>
关于javascript - 使用用户输入的文本设置 html 元素的 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39799860/