简体   繁体   English

突出 <li> 在js中的onClick函数中

[英]Highlight <li> in function onClick in js

I have a little issue. 我有一个小问题。

I want to highlight the answer when you click on it but the .style seem not working :( 当您单击它时,我想突出显示答案,但.style似乎不起作用:(

I have a part of html code like this: 我有一部分这样的html代码:

<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 谢谢答案

The style is not applied because jQuery objects don't have a style property. 由于jQuery对象没有style属性,因此未应用style You can use the css method instead: 您可以改用css方法:

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

Alternatively, you could use the underlying element (don't pass it to jQuery): 或者,您可以使用基础元素(不要将其传递给jQuery):

this.style.backgroundColor = "#FFFF00";

The second example will likely be more efficient. 第二个示例可能会更有效。

Use this 用这个

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

instead of this 代替这个

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

在此处修复: http//jsfiddle.net/RichardTowers/99Zpz/

try: 尝试:

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

instead of : 代替 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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