admin管理员组文章数量:1025498
I'm trying to change my body's background color to a random color every time a nav-bar link is clicked.
I got it so that a random color gets changed the first time a link is clicked - but when a different link is clicked, nothing changes.
Any tips?
HTML:
<div class="nav">
<ul id="show">
<li id="link1"><a href="#portfolio">Portfolio</li></a>
<li id="link2"><a href="#about">About</li></a>
<li id="link3"><a href="#email">Contact</li></a>
<hr/>
</ul>
</div>
JS:
$(function() {
var colors = ['red', 'blue', 'green', 'grey'];
var randColor = colors[Math.floor(Math.random()*colors.length)];
$('.nav a').click(function() {
$('body').css('background-color', randColor);
});
});
I'm trying to change my body's background color to a random color every time a nav-bar link is clicked.
I got it so that a random color gets changed the first time a link is clicked - but when a different link is clicked, nothing changes.
Any tips?
HTML:
<div class="nav">
<ul id="show">
<li id="link1"><a href="#portfolio">Portfolio</li></a>
<li id="link2"><a href="#about">About</li></a>
<li id="link3"><a href="#email">Contact</li></a>
<hr/>
</ul>
</div>
JS:
$(function() {
var colors = ['red', 'blue', 'green', 'grey'];
var randColor = colors[Math.floor(Math.random()*colors.length)];
$('.nav a').click(function() {
$('body').css('background-color', randColor);
});
});
Share
Improve this question
asked Sep 30, 2015 at 0:56
codinginnewyorkcodinginnewyork
1,1383 gold badges12 silver badges17 bronze badges
2
-
2
Your HTML structure is invalid. It’s supposed to be
<li><a>…</a></li>
and not<li><a>…</li></a>
. And remove the<hr/>
inside the<ul>
.<ul>
s and<ol>
s can only have<li>
s as child elements. – Sebastian Simon Commented Sep 30, 2015 at 1:00 - Based on your function I made this codepen.io/eirenaios/pen/EmYBBP – Suresh Karia Commented Apr 12, 2017 at 7:43
2 Answers
Reset to default 3Because you only generate one random number and keep using it, move it inside of the click method.
In each click you need to find a new random color, in your case the randColor
variable is updated only once on the dom ready, in the click handle the same value is used all the time.
$(function() {
var colors = ['red', 'blue', 'green', 'grey'],
color;
$('.nav a').click(function() {
var randColor;
do {
randColor = colors[Math.floor(Math.random() * colors.length)];
} while (color == randColor);
$('body').css('background-color', randColor);
color = randColor;
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="nav">
<ul id="show">
<li id="link1"><a href="#portfolio">Portfolio</a></li>
<li id="link2"><a href="#about">About</a></li>
<li id="link3"><a href="#email">Contact</a></li>
</ul>
<hr/>
</div>
I'm trying to change my body's background color to a random color every time a nav-bar link is clicked.
I got it so that a random color gets changed the first time a link is clicked - but when a different link is clicked, nothing changes.
Any tips?
HTML:
<div class="nav">
<ul id="show">
<li id="link1"><a href="#portfolio">Portfolio</li></a>
<li id="link2"><a href="#about">About</li></a>
<li id="link3"><a href="#email">Contact</li></a>
<hr/>
</ul>
</div>
JS:
$(function() {
var colors = ['red', 'blue', 'green', 'grey'];
var randColor = colors[Math.floor(Math.random()*colors.length)];
$('.nav a').click(function() {
$('body').css('background-color', randColor);
});
});
I'm trying to change my body's background color to a random color every time a nav-bar link is clicked.
I got it so that a random color gets changed the first time a link is clicked - but when a different link is clicked, nothing changes.
Any tips?
HTML:
<div class="nav">
<ul id="show">
<li id="link1"><a href="#portfolio">Portfolio</li></a>
<li id="link2"><a href="#about">About</li></a>
<li id="link3"><a href="#email">Contact</li></a>
<hr/>
</ul>
</div>
JS:
$(function() {
var colors = ['red', 'blue', 'green', 'grey'];
var randColor = colors[Math.floor(Math.random()*colors.length)];
$('.nav a').click(function() {
$('body').css('background-color', randColor);
});
});
Share
Improve this question
asked Sep 30, 2015 at 0:56
codinginnewyorkcodinginnewyork
1,1383 gold badges12 silver badges17 bronze badges
2
-
2
Your HTML structure is invalid. It’s supposed to be
<li><a>…</a></li>
and not<li><a>…</li></a>
. And remove the<hr/>
inside the<ul>
.<ul>
s and<ol>
s can only have<li>
s as child elements. – Sebastian Simon Commented Sep 30, 2015 at 1:00 - Based on your function I made this codepen.io/eirenaios/pen/EmYBBP – Suresh Karia Commented Apr 12, 2017 at 7:43
2 Answers
Reset to default 3Because you only generate one random number and keep using it, move it inside of the click method.
In each click you need to find a new random color, in your case the randColor
variable is updated only once on the dom ready, in the click handle the same value is used all the time.
$(function() {
var colors = ['red', 'blue', 'green', 'grey'],
color;
$('.nav a').click(function() {
var randColor;
do {
randColor = colors[Math.floor(Math.random() * colors.length)];
} while (color == randColor);
$('body').css('background-color', randColor);
color = randColor;
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="nav">
<ul id="show">
<li id="link1"><a href="#portfolio">Portfolio</a></li>
<li id="link2"><a href="#about">About</a></li>
<li id="link3"><a href="#email">Contact</a></li>
</ul>
<hr/>
</div>
本文标签: Change background to random colors on clickJavascriptjQueryStack Overflow
版权声明:本文标题:Change background to random colors on click - JavascriptjQuery - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745628412a2160007.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论