Javascript 定位警告来源的最佳方法:传递给 getElementById() 的空字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10790435/
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-08-24 02:56:39  来源:igfitidea点击:

Best way to locate source of Warning: Empty string passed to getElementById()

javascriptjquery

提问by Terry

Warning: Empty string passed to getElementById().
Source File: chrome://browser/content/browser.xul
Line: 0

警告:传递给 getElementById() 的空字符串。
源文件:chrome://browser/content/browser.xul
行:0

Some days ago I started getting the above message while developing my site. Things are working as they should (or at least I think they are), but I want to find out where this is coming from. I use jQuery so do not use getElementById()directly. Plus I have Firebug and the Web Developer extension for Firefox running.

几天前,我在开发我的网站时开始收到上述消息。事情正在按他们应该的方式工作(或者至少我认为他们是),但我想知道这是从哪里来的。我使用 jQuery,所以不要getElementById()直接使用。另外,我有 Firebug 和运行 Firefox 的 Web Developer 扩展。

I could laboriously put in code to check for an empty string being given to a jQuery selection or maybe look into jQuery itself, but my skills are minimal, so I'm asking here if anyone has offhand a good idea for quickly locating the source of the warning messages.

我可以费力地输入代码来检查给 jQuery 选择的空字符串,或者查看 jQuery 本身,但我的技能很少,所以我在这里问是否有人有一个快速定位源的好主意警告信息。

采纳答案by lanzz

Source File: chrome://browser/content/browser.xul

This means that the error is in a Firefox extension, not in your code.

这意味着错误在 Firefox 扩展中,而不是在您的代码中。

回答by user1898553

If you are querying by '#' selector -- ensure the selector is unique (as it should be) or you will end up with this error in ff

如果您通过 '#' 选择器进行查询 - 确保选择器是唯一的(应该如此),否则您最终会在 ff 中出现此错误

回答by MattyGT

Was receiving this warning in Firefox.

在 Firefox 中收到此警告。

I just came across this warning and found that there were labels for input fields but the labels for attribute was not set/empty so just completing the for attribute fixed this issue for me.

我刚刚遇到这个警告,发现输入字段有标签,但属性的标签没有设置/为空,所以只需完成 for 属性就为我解决了这个问题。

// Label's for attribute not set which caused the warning in FF

e.g. <label for=''>Text</label><input type="text" name="text" id="text" value="" />

回答by DoctorFox

In my case this was caused by Firefox performing HTML5 checks on the input fields on my form. Once the "required" property was removed from the form elements everything seemed to work fine again.

就我而言,这是由 Firefox 对表单上的输入字段执行 HTML5 检查引起的。一旦从表单元素中删除了“必需”属性,一切似乎又可以正常工作了。

This was something brought about by the MVC framework I use which generates these properties based on the model validation rules that require a field to be non-empty.

这是由我使用的 MVC 框架带来的,它根据需要字段为非空的模型验证规则生成这些属性。

回答by suchislife

This answer was really helpful to me in finding why it was happening so I decided to share.

这个答案对我找出它发生的原因真的很有帮助,所以我决定分享。

I've placed the following code OUTSIDEdocument.readyand got this error.

我已将以下代码放在OUTSIDE 之外document.ready并出现此错误。

// Enable Line Items ONLY AFTER general info is filled out!
$( "#client_estimate_continue_next" ).click(function(e) {
//e.preventDefault();
console.log(this.id + ' click event fired.');

// DO SOMETHING...

});

I was able to fix the error by simply placing it INSIDEdocument.ready

我可以通过简单地将它放在INSIDE来修复错误document.ready

Additional info:

附加信息:

I got the error but in my case it was coming from my own scriptaccording to FireFox.

我收到了错误,但在我的情况下,根据 FireFox,它来自我自己的脚本

I believe I got the error because I made a reference to the id of an element that was not yet fully rendered eg. NOT ready.

我相信我得到了错误,因为我引用了尚未完全呈现的元素的 id,例如。还没准备好

Warning: Empty string passed to getElementById().

警告:传递给 getElementById() 的空字符串。