2014-11-03 58 views
-1

我找了一段代碼,讓我改變點擊的對象的CSS,像這樣的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); 
}}) 
} 

回答

1

你需要css('key', 'value')超載jQuery.css

$('.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); 
    } 
}) 
+0

的感謝!這真的很有幫助! – Max 2014-11-03 16:29:59

1

首先,你不應該直接操縱CSS。通過添加/刪除類名來更改邊框顏色,並在外部CSS中設置這些類的樣式。

如果你真的改變與JavaScript中的CSS,你需要給它的屬性和價值,而不是一個字符串:

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

是的,這是:) – 2014-11-03 16:18:17

+0

非常感謝! – Max 2014-11-03 16:30:27