严格的文档类型阻止访问FireFox中的DOM变量 [英] Strict doctype preventing access to DOM variable in FireFox

查看:144
本文介绍了严格的文档类型阻止访问FireFox中的DOM变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是一个众所周知的东西或什么新的Firefox的版本,它只是更新自己 - 但无论如何,我不知道如何谷歌的这个问题,所以我会在这里问它。

我在我的DOM中有一个DIV,我试图通过id直接访问,最简单的形式是这样的:

  alert(btnTest.id); 

这在所有浏览器中都能正常工作,但是在Firefox中导致浏览器出现问题一个奇怪的破碎的状态。



我得到的错误是'btnTest没有定义'。我没有在Safari,Internet Explorer或Chrome中得到这个错误。



我假设我有不匹配的HTML标签或javascript大括号或其他东西丢失。最终剥离一切后,我试图删除DOCTYPE。突然在Firefox(v 3.0.10)中,它开始按预期返回正确的ID。



发生了什么事情! ??当然,严格模式应该允许我访问指定的元素,如果不是,那么为什么所有其他浏览器都让我使用。


$ b

注意:我可以用$('#btnTest')[0] .id轻松解决这个问题,这是我现在要做的,直到我能找出更好的解决方案。

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Strict // ENhttp://www.w3.org/TR/xhtml1 /DTD/xhtml1-strict.dtd\"> 

< head>

< script type =text / javascript
src =http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js >< /脚本>

< script>
$($ {
alert(通过jQuery获取的ID:+ $('#btnTest')[0] .id);
alert(通过访问全局变量:+ btnTest.id);
});

< / script>

< / head>
< body>
< div id =btnTest>
< / div>
< / body>
< / html>


解决方案

应该在具有 id 属性的元素的全局脚本范围中建立。这被认为是无法污染全球名称空间,并可能导致混淆错误

Firefox为了IE兼容性的目的,在怪癖模式下运行时建立引用。 Johnny Stenback在关于添加此支持的错误的第三个评论中解释了为什么这个在标准模式下不支持:

lockquote

这个特性确实会影响标准的
兼容的代码,比如
检查全局
变量的存在[ sic ]只能设置一次。用
这个改变,那个varible[ sic ]现在可能是
a对
文档中元素的引用,并且代码可能不工作


这就是我们决定让
这个怪癖的原因。



I don't know if this is a well known 'thing' or something new in whatever version of Firefox it just updated itself too - but in either case I have no idea how to google for this question so I'll have to ask it here.

I have a DIV in my DOM that I am trying to directly access by id, in the most simplest form like this:

 alert(btnTest.id);

This works fine in all browsers, but was causing issues in firefox that actually led to the browser being in a strange 'broken' state.

The error I was getting was 'btnTest is not defined'. I did not get this error in Safari, Internet Explorer or Chrome.

I assumed I had mismatched HTML tags, or javascript curly braces or something else missing. Eventually after stripping everything out I tried removing the DOCTYPE. Suddenly in Firefox (v 3.0.10) it started returning the correct ID as expected.

What is going on!!! ?? Surely 'strict' mode should allow me to access named elements, and if not then why do all the other browsers let me.

Note: I can easily work around it with $('#btnTest')[0].id, which is what I'm going to have to do now until i can figure out a better solution.

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" class="blueCircles">

 <head>

    <script type="text/javascript"
            src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

    <script>
        $(function() {
            alert("ID retrieved through jQuery: " + $('#btnTest')[0].id);
            alert("ID retrieved by accessing global variable: " + btnTest.id);
        });

    </script>

</head>
<body>
    <div id="btnTest">
    </div>
</body>
</html>

解决方案

There is nothing in any W3C specification that says object references should be established in the global scripting scope for elements with id attributes. This is considered to uneccessarily pollute the global namespace and can result in confusing errors.

Firefox establishes the references when running in quirks mode for the purposes of IE compatibility. Johnny Stenback explains in the third comment on the bug for adding this support why this isn't supported in standards mode:

This feature does affect standard compliant code that for instance checks for the existance [sic] of a global variable to set it only once. With this change, that "varible" [sic] may now be a reference to an element in the document, and the code may not work the way the developer intended.

That's the reason we decided to make this quirks only.

这篇关于严格的文档类型阻止访问FireFox中的DOM变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆