javascript - Highlight <li> in function onClick in js - Stack Overflow

I have a little issue.I want to highlight the answer when you click on it but the .style seem not worki

I have a little issue.

I want to highlight the answer when you click on it but the .style seem not working :(

I have a part of html code like this:

<div id="answers-type1">
    <ol>
        <img src="" id="answer-a-img" /><li id="answer-a" ></li>
        <img src="" id="answer-b-img" /><li id="answer-b" ></li>
        <img src="" id="answer-c-img" /><li id="answer-c" ></li>
        <img src="" id="answer-d-img" /><li id="answer-d" ></li>
    </ol>
</div>

and in my script i got this:

$("#answers-type1 li").click(function() {
     $(this).style = "background-color: #FFFF00;";
     if ($(this).text() == goodAnswer)
     {
         alert("JAYJAY");
     }
     else
     {
         alert("pas JAYJAY");
     }
});

the function is well called on each click but the background is not changing i don't understand why :/

I looked over internet and saw a lot of people doing like that

thanks for answers

I have a little issue.

I want to highlight the answer when you click on it but the .style seem not working :(

I have a part of html code like this:

<div id="answers-type1">
    <ol>
        <img src="" id="answer-a-img" /><li id="answer-a" ></li>
        <img src="" id="answer-b-img" /><li id="answer-b" ></li>
        <img src="" id="answer-c-img" /><li id="answer-c" ></li>
        <img src="" id="answer-d-img" /><li id="answer-d" ></li>
    </ol>
</div>

and in my script i got this:

$("#answers-type1 li").click(function() {
     $(this).style = "background-color: #FFFF00;";
     if ($(this).text() == goodAnswer)
     {
         alert("JAYJAY");
     }
     else
     {
         alert("pas JAYJAY");
     }
});

the function is well called on each click but the background is not changing i don't understand why :/

I looked over internet and saw a lot of people doing like that

thanks for answers

Share edited May 24, 2012 at 17:14 James Allardice 166k22 gold badges334 silver badges315 bronze badges asked May 24, 2012 at 15:51 John SmithJohn Smith 1,1721 gold badge12 silver badges31 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

Use this

$(this).css("background-color","#FFFF00");

instead of this

this.style = "background-color: #FFFF00;";

The style is not applied because jQuery objects don't have a style property. You can use the css method instead:

$(this).css("background-color","#FFFF00");

Alternatively, you could use the underlying element (don't pass it to jQuery):

this.style.backgroundColor = "#FFFF00";

The second example will likely be more efficient.

Fixed here: http://jsfiddle/RichardTowers/99Zpz/

try:

$(this).css("background-color","#FFFF00");

instead of :

$(this).style = "background-color: #FFFF00;";

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745062030a4609021.html

相关推荐

  • javascript - Highlight &lt;li&gt; in function onClick in js - Stack Overflow

    I have a little issue.I want to highlight the answer when you click on it but the .style seem not worki

    18小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信