简体   繁体   中英

How to change the colour of a div element when clicked?

I have a this html page, Whenever the element with class name FreeSeat is clicked I want to change the colour of that div element.Below is my html page

<html>
 <head>
  <title>
   QuickBus
  </title>
  <link type="text/css" rel="stylesheet" href="Seat.css">
 </head>
 <body>
  <div id="Bus">
   <div class="Row">
    <div class="FreeSeat"  ></div>
    <div class="FreeSeat"  ></div>
    <div class="ResSeat"  ></div>
    <div class="ResSeat"  ></div>
    <div class="ResSeat"  ></div>
   </div>
  </div>
 <body>
</html>

It will be very helpful if anyone can help me out with this .

Considering that you want to use pure JS and not any library, you'd have to manually add event listeners to your classes. And it has been solved for a similar problem here

var freeclass = document.getElementsByClassName("FreeSeat");
var myFunction_Free = function() {
    this.style.color = "blue";
}
for(var i=0;i<freeclass.length;i++){
    freeclass[i].addEventListener('click', myFunction_Free, false);
}

But for your case, here's a working fiddle

You can use the following method to change the background color of an element by class:

const free_seat = document.getElementsByClassName('FreeSeat');
free_seat[0].style.backgroundColor = '#ff0';

Each element can be referenced by its index:

free_seat[0] // first div
free_seat[1] // second div

Therefore, we can create a function that will be called whenever the click event is delivered to the target:

const change_color = () => {
  this.style.backgroundColor = '#ff0';
};

for (let i = 0; i < free_seat.length; i++) {
  free_seat[i].addEventListener('click', change_color);
}

Note: You can also use document.querySelectorAll('.FreeSeat') to obtain a NodeList of elements of a certain class.

JQuery is amazing for these sorts of things.

Say you have a div with id 'box1'

<div id='box1'></div>

Style it with css

#box1 {
    width:100px;
    height:100px;
    background-color:white;
    border:1px solid black;
}

Using JQuery, you can make this call:

$( "#box1" ).click(function() {
  $('#box1').css('background-color', 'red');
});

And now whenever your div is clicked, the colour will change, you can customise this however much you like.

Here is a JSFiddle demo.

Also, since you didn't specify exactly what you want to change the colour of , in my example jquery, it is telling the browser that when a div with an id of box1 is clicked, change the background-color of the div with an id of box1 , you can change anything though.

If you have a <p> tag you can change that too when the div is clicked, hope this helped!

You can use simply the css focus pseudo-class for this:

#foo:focus {
    background-color:red;
}

<div id="foo" tabindex="1">hello world!</div>

Dont forget to set the tabindex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM