2014-09-20 51 views
0

我試圖改變使用Ajax和JavaScript,div背景顏色,但我的代碼是不工作:如何使用Javascript和Ajax更改div背景顏色?

代碼:

<script type="text/javascript"> 
    window.rowcount=function() { 
     var exam = new XMLHttpRequest(); 
     exam.onreadystatechange = function() { 
      if (exam.readyState == 4) {        
       var i=document.getElementById("newdata").innerHTML = exam.responseText; 
       if(i==1){ 
        document.getElementsById("newdata").style.backgroundColor = green;     
       } 

      } 
     } 
     exam.open("GET", "demo1.php?", true); 
     exam.send(null); 

</script> 

如果我錯了這段代碼?

任何幫助將不勝感激。

回答

1

問題是沒有定義​​。您可以使用"green"#00FF00

試試這個:

<script type="text/javascript"> 
    window.rowcount=function() { 
     var exam = new XMLHttpRequest(); 
     exam.onreadystatechange = function() { 
      if (exam.readyState == 4) { 


       var i=document.getElementById("newdata").innerHTML = exam.responseText; 
      if(i==1){ 
        document.getElementsById("newdata").style.backgroundColor = "#00FF00"; 

      } 

      } 
     } 
     exam.open("GET", "demo1.php?", true); 
     exam.send(null); 

    </script> 
+0

不工作 – 2014-09-20 13:22:02

+0

@satyamsharma可能是因爲'if(i == 1)'部分 – Progo 2014-09-20 19:02:09

0

你忘了布萊希特和綠色需求是一個字符串,所以:

<script type="text/javascript"> 
    window.rowcount=function(){ 
    var exam=new XMLHttpRequest(); 
    exam.onreadystatechange=function(){ 
     if(exam.readyState==4){ 
     var i=document.getElementById("newdata").innerHTML=exam.responseText; 
     } 
     if(i==1){ 
     document.getElementsById("newdata").style.backgroundColor="green"; 
     } 
    } 
    } 
    exam.open("GET", "demo1.php?", true); 
    exam.send(); 
</script> 
0

通過可變打字綠色傳遞。輸入「Green」代替