最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Change background to random colors on click - JavascriptjQuery - Stack Overflow

matteradmin6PV0评论

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
Add a ment  | 

2 Answers 2

Reset to default 3

Because 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>

Post a comment

comment list (0)

  1. No comments so far