javascript - jQuery: is it possible to make ID/class-less elements change on click? -
example css:
div { background-color: green; }
example html:
<div>apple 1</div> <div>apple 2</div> <div>apple 3</div>
is possible bind jquery clicks these, clicked div change background color blue, , others revert default color?
i'm thinking like:
$("div").click(function() { // first change default color $("div").each( background-color: green; } // change clicked div blue how clicked element in here dont know. });
use $(this)
refer clicked element:
$("div").click(function() { // first change default color $("div").css('background-color', 'green') // change clicked div blue $(this).css('background-color','blue'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div>apple 1</div> <div>apple 2</div> <div>apple 2</div>
further reading:
Comments
Post a Comment