简体   繁体   English

如何操作当前对象的CSS

[英]How do I manipulate the CSS of the current Object

I am looking for a piece of code that lets me change the CSS of the clicked Object, like so: 我正在寻找一段代码,让我可以更改单击的对象的CSS,如下所示:

//Click & Lock
 $('.pix').click(function(){
  if(questionLock==false){questionLock=true;    
    //correct answer
  if(this.id==rnd){
   $(this).css('border-color:green'); 
   score++;
   }
    //wrong answer  
  if(this.id!=rnd){
   $(this).css('border-color:red');
  }
  setTimeout(function(){changeQuestion()},1000);
 }})
}   

You need css('key', 'value') overload of jQuery.css 您需要jQuery.css css('key', 'value')重载

$('.pix').click(function() {
    if (questionLock == false) {
        questionLock = true;
        //correct answer
        if (this.id == rnd) {
            $(this).css('border-color', 'green');
            score++;
        }
        //wrong answer  
        if (this.id != rnd) {
            $(this).css('border-color', 'red');
        }
        setTimeout(function() {
            changeQuestion()
        }, 1000);
    }
})

First and foremost, you shouldn't be manipulating CSS directly. 首先,您不应该直接操作CSS。 Change the border color by adding/removing class names, and styling those classes in your external CSS. 通过添加/删除类名称,并在外部CSS中设置这些类的样式来更改边框颜色。

If you really want to change the CSS with JavaScript, you need to give it a property and value, not a single string: 如果您确实想使用JavaScript更改CSS,则需要为其提供属性和值,而不是单个字符串:

$(this).css('border-color', 'red');

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

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