开发者

How to change background-color on click with jQuery? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.

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 question

I 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....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜