admin管理员组文章数量:1399796
I need help with my html work. I'm pretty new to javascript and I had a work asking me to "onclick changes the color of the text in our ordered list to change to a random color."
The paragraph that needs to be changed is id=p1
, and so far all I got is
<script>
// Random Colors
function randomColors() {
}
</script>
and
<div>
<button type="button" id="b1" class="button" onclick="randomColors()">Button 1</button>
</div>
I need help with my html work. I'm pretty new to javascript and I had a work asking me to "onclick changes the color of the text in our ordered list to change to a random color."
The paragraph that needs to be changed is id=p1
, and so far all I got is
<script>
// Random Colors
function randomColors() {
}
</script>
and
<div>
<button type="button" id="b1" class="button" onclick="randomColors()">Button 1</button>
</div>
Share
Improve this question
edited Mar 30, 2015 at 23:54
Jawa
2,3326 gold badges34 silver badges39 bronze badges
asked Mar 30, 2015 at 21:43
Anthony Anthony
31 gold badge1 silver badge3 bronze badges
2 Answers
Reset to default 2Paul Irish has a few examples of a random color generator for javascript on his website here.
Which, can be implemented fairly simply...
Example
function randomize() {
document.getElementById('p1').style.color = randomColors();
}
// random colors - taken from here:
// http://www.paulirish./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
here is a nice one that keeps the same lightness and saturation:
function getRndColor() {
return 'hsl(' + (360 * Math.random()) + ',50%,50%)'; // H,S,L
}
本文标签: javascriptChange color of the text on click to random color (Button)Stack Overflow
版权声明:本文标题:javascript - Change color of the text on click to random color (Button) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744221220a2595875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论