我的页面有两个textareas和一个div。一个textarea用于输入html,另一个用于输入css。当一个按钮被点击时,我会调用一个javascript函数来显示div内的css和html。使用用户输入的文本设置html元素的CSS
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的文字?
编辑:我应该提到,我希望能够在css textarea中放入类似 .classExample {text-align:center}的东西并使其适用。
反正'div.style.cssText = css.value'会让你键入CSS在输入 – adeneo
不幸的是,没有工作 – Sidward
你确定** div.style.cssText =“background-color:red”; **不起作用? – JonSG