我有一个按钮,单击它会将文本的颜色更改为红色。
下面是一些基本的JS:
function colorChange() {
document.getElementById('text').style.color = "red";
}
和 HTML:
<p id="text">some text</p>
<button type="button" onclick="colorChange()">red</button>
不适用于 JSfiddle 或 Codepen。但是 - 当我制作一个相同的本地 HTML 文件时,它会按预期工作。
为什么这个 vanilla Javascript 在 Codepen/JSfiddle 中不起作用的原因是什么?不涉及任何库(jQuery、React 等)。我的第一个直接想法是在 JSFiddle 中,您可以在负载类型的设置中设置一个属性。我将其设置为onLoad
但仍然无法正常工作。Codepen 看起来并不提供对此的控制。
JSFiddle: https ://jsfiddle.net/mo5pro4a/1/
代码笔: https ://codepen.io/anon/pen/YVYZXj
更新:Codepen 实际上看起来还不错 - 现在主要是 jsfiddle 的问题。