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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:41:14  来源:igfitidea点击:

TypeError: $(...).style is undefined - bug or my fault?

javascriptjquerycssfirefox

提问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

you are mixing javascript with jquery.

您正在将 javascript 与 jquery 混合使用。

In jquery you have to use css()to make it work like this:

在 jquery 中,您必须使用css()使其像这样工作:

 $("#gallery-container").css("background","black");

回答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');
});