javascript document.getElementById 在 Firefox 上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21187264/
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
document.getElementById does not work on Firefox
提问by Charles
Firefox returns null when I try to call document.getElementById on an element.
当我尝试在元素上调用 document.getElementById 时,Firefox 返回 null。
Here is the call
这是电话
document.getElementById('interaction4793')
However the element is present in the DOM. I can find it
但是该元素存在于 DOM 中。我能找到它
<interaction id="interaction4793">
<action id="action3268" trigger="enter" type="hover" />
<reaction delay="0" id="reaction3709" options="reloadOnly" target="page0001" transition="none" type="showPage" />
</interaction>
Even when I select it from the inspector and use the $0trick it returns null:
即使我从检查器中选择它并使用$0技巧,它也会返回 null:
document.getElementById(console.log($('#interaction102').length); // returns 0
console.log($('[id="interaction102"]').length); // returns 1
.id)
When I try to get the elements from getElementsById it works.
当我尝试从 getElementsById 获取元素时,它可以工作。
EDIT: Chrome and Safari do not return null. I tested Firefox (version 26) on Window and MacOS both return null.
编辑:Chrome 和 Safari 不返回 null。我在 Window 和 MacOS 上测试了 Firefox(版本 26)都返回 null。
EDIT2:
I think Firefox do not understand that the attribute id
is the id. When I search by attribute with jquery I can find the interaction:
EDIT2:我认为 Firefox 不明白该属性id
是 id。当我使用 jquery 按属性搜索时,我可以找到交互:
回答by MarcoGarzini
The question is already answered here: JavaScript getElementByID() not working
问题已经在这里回答:JavaScript getElementByID() not working
Sometimes happen when the content of your website is not already loaded and crash.
有时当您的网站内容尚未加载并崩溃时会发生。
Hope to be helpful
希望有帮助
EDIT1: You can also try this: var yourvar= $("#interaction4793")[0];It works for me
EDIT1:你也可以试试这个:var yourvar= $("#interaction4793")[0]; 这个对我有用