2012-10-31 51 views
4

我試圖做一個網站,但我遇到了問題。當我在Chrome中運行它時:「無法設置未定義的屬性'backgroundColor'」。我不明白爲什麼我得到這個。我的var不是一個數組(只是一個id)。我一直在使用Google搜索2小時,但沒有任何幫助。Javascript - 無法設置屬性'backgroundColor'

這裏是我的HTML代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"> 
<head> 
    <title>Folio</title> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
    <link rel="stylesheet" type="text/css" href="style.css"/> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
    <script type="text/javascript" src="anim2.js"></script>  
</head> 
<body> 
    <div id="tout"> 
     <img id="menu_jaune" src="menu_jaune.png" alt="le menu"/></td> 
     <div id="bandeau_graphisme"> 
      <img id="dev" src="dev3.png" alt="image de couverture du site"/> 
     </div> 
     <div id="surmenu"> 
      <div id="menu"> 
       <img id="ar" src="ar.png" alt="ar"/> 
      </div> 
     </div> 
     <div id="go"> 
      <p id="wtf">HERE WE GO!</p> 
     </div> 
     <div id="reste"> 
     </div> 
    </div> 
</body> 

我的Javascript代碼:

$(document).ready 
(
function() 
{ 
    $("#go").bind("click", remonte) 
} 
); 

function remonte(){ 
$("#menu").animate({marginTop: "-460"}, 500); 
$("#ar").animate({marginTop: "120"}, 500); 
$("#reste").animate({height: "500", marginTop: "-50"},400); 
$("#go").remove(); 
setTimeout(charge, 510);  
} 

function charge(){ 
    $('#reste').load("about.html",'',montreNouveauContenu); 
    $("#surmenu").style.backgroundColor="transparent"; //HERE 
    $("#menu_jaune").animate({width: "900"}, 500); 
} 

function montreNouveauContenu() { 
$('#content').show('normal'); 
} 
function suppr(){ 
$("#dev").remove(); 
$("#reste").remove(); 
$("#content").remove(); 
$("#bandeau_graphisme").animate({height: "255"},500); 
} 

預先感謝您。

回答

15

這是因爲jQuery對象沒有style屬性。

// ---------v----??? 
$("#surmenu").style.backgroundColor="transparent"; //HERE 

它應該是這樣的:

$("#surmenu").css("backgroundColor", "transparent"); 

如果你想使用DOM API,你可以做一個傳統的DOM的選擇:

document.getElementById("surmenu").style.backgroundColor="transparent"; 
+0

它的作品,非常感謝你! –

+0

不客氣。 –

+0

因爲我沒有看到變化而感到頭痛......原來啓用了緩存(新建,並忘記在DevTools中關閉它)。只要值得指出,以防人們遇到同樣的問題。 –

3

這是一個jQuery對象,而不是一個元素,所以它沒有style財產。你想這樣的:

$("#surmenu")[0].style.backgroundColor="transparent"; //HERE 

或本...

$("#surmenu").get(0).style.backgroundColor="transparent"; //HERE 

或本...

$("#surmenu").css('background-color', 'transparent'); //HERE