2012-10-31 50 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