最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

I first wanted only to change the background of one element but then this came across:

TypeError: $(...).style is undefined (in the Firefox Console)

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:

$(document).ready(function(){
   $("#gallery-container").style.background = "black";
});

I first wanted only to change the background of one element but then this came across:

TypeError: $(...).style is undefined (in the Firefox Console)

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:

$(document).ready(function(){
   $("#gallery-container").style.background = "black";
});
Share Improve this question edited Jun 30, 2022 at 10:45 Patrick Yoder 1,1535 gold badges16 silver badges22 bronze badges asked May 24, 2014 at 19:39 SteveSteve 1431 gold badge1 silver badge6 bronze badges 4
  • 10 it's your fault, style is a property of a pure JS element object not of a jQuery object, try $("#gallery-container")[0].style instead. However you should use the .css jQuery method and forget about the style property. – King King Commented May 24, 2014 at 19:41
  • rtfm learn.jquery.com/using-jquery-core/jquery-object – chiliNUT Commented May 24, 2014 at 19:46
  • $("#gallery-container") is a jQuery object and you are trying to style a dom node with your script, Either turn it to a dom node with including [0] to it like $("#gallery-container")[0] or as suggested use the jQuery's .css() method to style it. – Jai Commented May 24, 2014 at 19:47
  • Thanks for asking! As a back-end coder I just copied a function toggle() to show/hide DOM objects, and it turned out only [0] was missing. dustindiaz.com/seven-togglers under "toggling multiple objects" – Marcos Commented Jan 12, 2015 at 13:54
Add a comment  | 

4 Answers 4

Reset to default 13

you are mixing javascript with jquery.

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

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

Try this,

$(document).ready(function(){
   $("#gallery-container").css('background-color','black');
});

This is the correct jQuery method:

$(document).ready(function(){
   $("#gallery-container").css('background-color','black');
});

You need the existence by the following:

$(document).ready(function(){
   let g = $("#gallery-container");
   if(typeof g !== undefined) g.css('background','black');
});
发布评论

评论列表(0)

  1. 暂无评论