javascript TypeError: $(...).style 未定义 - 错误还是我的错?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23848933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: $(...).style is undefined - bug or my fault?
提问by Steve
First hi everyone.. i just wanted to make a simple website for a friend and i thougt to add some effect.. but jeah.. first i wanted only to change the background of one element but then this came across:
首先大家好。
TypeError: $(...).style is undefined (in the Firefox Console)
TypeError: $(...).style 未定义(在 Firefox 控制台中)
HTML:
HTML:
<!DOCTYPE html>
<html lang="DE">
<head>
<meta charset="utf-8"/>
<title>Laura Sack - Offizielle Webseite</title>
</head>
<body>
<div id="gallery-container" class="gallery-container cf"></div>
<script src="js/jquery.js"></script>
<script src="js/main.js"></script>
</body>
</html>
Javascript:
Javascript:
$(document).ready(function(){
$("#gallery-container").style.background = "black";
});
回答by Ehsan Sajjad
回答by Rashmin Javiya
Try this,
试试这个,
$(document).ready(function(){
$("#gallery-container").css('background-color','black');
});
回答by myfunkyside
This is the correct jQuery method:
这是正确的 jQuery 方法:
$(document).ready(function(){
$("#gallery-container").css('background-color','black');
});