2015-03-30 263 views
0

我需要帮助我的HTML工作。我对JavaScript非常陌生,我有一项工作要求我“点击更改我们的有序列表中的文本颜色,以更改为随机颜色。”改变点击颜色为随机颜色的文本颜色(按钮)

需要更改的段落是id=p1,到目前为止,我的一切是

<script> 
// Random Colors 
function randomColors() { 
}  
</script> 

<div> 
    <button type="button" id="b1" class="button" onclick="randomColors()">Button 1</button> 
</div> 

回答

1

保罗爱尔兰有一个随机颜色生成的JavaScript上的几个例子他的网站here.

其中,可以相当简单地实现...

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>

HTH

+0

我知道这是简单的 但我爱你 – Anthony 2015-03-30 22:08:28

0

这里是一个很好的一个,保持相同的亮度和饱和度:

function getRndColor() { 
    return 'hsl(' + (360 * Math.random()) + ',50%,50%)'; // H,S,L 
}