我的HTML工作需要帮助。我对javascript很陌生,我有一项工作要求我“ onclick更改我们的有序列表中文本的颜色以更改为随机颜色”。
需要更改的段落是id=p1
,到目前为止,我所得到的只是
<script>
// Random Colors
function randomColors() {
}
</script>
和
<div>
<button type="button" id="b1" class="button" onclick="randomColors()">Button 1</button>
</div>
保罗·爱尔兰(Paul Irish)在他的网站上提供了一些用于JavaScript的随机颜色生成器的示例。
可以很简单地实现...
例子
function randomize() {
document.getElementById('p1').style.color = randomColors();
}
// random colors - taken from here:
// http://www.paulirish.com/2009/random-hex-color-code-snippets/
function randomColors() {
return '#' + Math.floor(Math.random() * 16777215).toString(16);
}
<div>
<p id="p1">Hello World</p>
</div>
<button id="b1" onclick="randomize()">Click Me!</button>
高温超导
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句