2014-01-08 4 views
0

배경색을 변경하고 싶지만 빨강으로 설정할 수 있습니다. 빨간색은 파란색으로 바뀌면 작동하지 않습니다. true를 반환하지 않으면. 나는 빨간 글씨 - "rgb(256, 0, 0)"을 쓰고 아직도 작동하지 않습니다. 어떻게해야합니까? 이처럼배경색 확인 및 변경

if($("#row-"+id).css("background-color")!="red") 
{ 
    $("#row-"+id).css("background-color", "red"); 
} 
else if($("#row-"+id).css("background-color")== "red") 
{ 
    alert("don't show this"); 
    $("#row-"+id).css("background-color", "blue"); 
} 
+1

적색 BTW 정확한 값은 'RGB있다 (255, 0, 0)'이 아닌 'RGB (256, 0 , 0)'. – KunJ

+0

console.log를 사용하여 값이 무엇인지 확인하십시오. –

+2

"else if"를 왜 사용하고 있습니까? "빨강"(첫 번째 경우)이 아니라면 항상 빨간색이므로 필요한 경우 두 번째가 없으므로 다른 요소만으로 충분합니다. – Fabricio

답변

1

:

크롬 $에서
var $div = $('div'); 
var bg = $div.css('background-color'); 

if (bg !== 'rgb(255, 0, 0)') { 
    $div.css('background-color', 'red'); 
} 
else { 
    $div.css('background-color', 'blue'); 
} 

http://jsfiddle.net/k9jYE/

0

(선택) .CSS는 ("배경색")는 RGB 값을 반환합니다.

따라서 당신은이 작업을 수행 할 수 있습니다

<p id="row" style="background-color:red">Hello</p> 

<script> 
    var bgColor = $("#row").css("background-color"); 

    if(bgColor === "rgb(255, 0, 0)"){ 
    $("#row").css("background-color", "blue"); 
    } else { 
    $("#row").css("background-color", "red"); 
    } 
</script> 

그러나이의 jQuery 특정되지 않습니다. jQuery는 브라우저가 계산 된 값 (예 : getComputedStyle 또는 currentStyle)에 대해 브라우저에서 제공하는 값을 반환합니다. 브라우저가 제공하는 것은 브라우저가 사용하고자하는 표기 형식 일 수 있습니다.

여기서 참조 : Why does jQuery .css('background-color') return rgba(0,0,0,0) for 'transparent'?