How to change background-color on click with jQuery? [closed]
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this questionI have a demo here.
开发者_开发技巧I want to accomplish something similar to this.
I'm not able to figure out where am I going wrong.
Based on your demos, you is just changing background-color
of spans, regardless of their id
, so those ifs are very unnecessary. Just it is working well:
$(".AccordionPanelContent span").click(function() {
$(".AccordionPanelContent span").css("background-color", "white");
$(this).css("background-color", "red");
});
DEMO
The code used:
$("span[id*='select']").click(function() {
$("span[id*='select']").removeClass('selected');
$(this).addClass('selected');
});
And a bit of CSS:
.selected{
background:#f00;
}
You can see the $("span[id*='select']")
, it restricts the selector to only the ID elements that have a name containing the text: select....
精彩评论