简体   繁体   English

Firefox JavaScript 错误“未定义”(div Id)。控制台中的样式

[英]Firefox JavaScript error “undefined” (div Id).style in console

I am using JavaScript to resize and reposition page elements to fit the browser window.我正在使用 JavaScript 调整页面元素的大小和位置以适应浏览器窗口。 My code works perfectly in Chrome, Safari, and Internet Explorer 9 , but breaks in Firefox.我的代码在 Chrome、Safari 和Internet Explorer 9 中完美运行,但在 Firefox 中会中断。 The console gives me two errors: "TypeError: sidebar.style is undefined" and "TypeError: content.style is undefined" .控制台给了我两个错误: "TypeError: sidebar.style is undefined""TypeError: content.style is undefined"

I certainly do have two divs with Id's named "sidebar" and "content" and am using both of the Id's successfully in CSS.我当然有两个 Id 名为“侧边栏”和“内容”的 div,并且我在 CSS 中成功地使用了这两个 Id。 In what way could they be undefined?它们以什么方式可以是未定义的?

Adding to the confusion, a duplicate of this script is used on another page in the site, but with another Id specific to that page.更令人困惑的是,该脚本的副本在站点的另一个页面上使用,但具有特定于该页面的另一个 Id。 It works with no problems.它没有问题。 What might be causing this problem?什么可能导致这个问题?

You don't show your code, but given the wording of your question and the title with "(div Id).style in console" I suspect you are trying to simply reference the elements directly in your JavaScript by using the ids as if they were variables.您没有显示您的代码,但考虑到您的问题的措辞和带有“(div Id).style in console”的标题,我怀疑您正试图通过使用 id 直接在 JavaScript 中简单地引用元素,就好像它们一样是变量。 Some browsers do allow this, though it can cause problems if you also have variables of the same name.某些浏览器确实允许这样做,但如果您还有同名的变量可能会导致问题。 Some browsers do not allow this.有些浏览器不允许这样。

You should get a reference to the elements using document.getElementById() :您应该使用document.getElementById()获取对元素的引用:

document.getElementById("sidebar").style
// NOT
sidebar.style

Of course you don't want to have to keep calling document.getElementById() over and over for the same element, so you can create a variable that keeps a reference to the element:当然,您不想为同一个元素一遍又一遍地调用document.getElementById() ,因此您可以创建一个变量来保存对元素的引用:

var theSidebar = document.getElementById("sidebar");
// then later to do something to the element's style
theSidebar.style.color = "blue";

Note also that JavaScript code that tries to reference elements from your HTML will only work if run after those elements have been parsed when the page loads.另请注意,尝试从您的 HTML 中引用元素的 JavaScript 代码只有在页面加载时解析这些元素之后才能运行。 So you need your code to either be in a script element included after the element(s) it accesses or to use a document ready or onload handler to delay executing the code until the whole page is parsed.因此,您需要将代码放在包含在它访问的元素之后的脚本元素中,或者使用文档就绪或加载处理程序延迟执行代码,直到整个页面被解析。

I ultimately resolved the issue by simply changing the ID of the div.我最终通过简单地更改 div 的 ID 解决了这个问题。 Evidently Firefox disliked 'sidebar' as an ID for some reason.显然,出于某种原因,Firefox 不喜欢将“侧边栏”作为 ID。 No other changes to the code were necessary.不需要对代码进行其他更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM