id名称是全局变量? [英] id names are global variables?

查看:89
本文介绍了id名称是全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做时:


< input id =" someName" type =" text">


< script type =" text / javascript">

alert(someName.nodeName);

< / script>

IE6和Firefox警报(INPUT)。为什么不是someName undefined?


为什么我不需要这样做:


getElementById(" someName")。nodeName;

When I do this:

<input id="someName" type="text">

<script type="text/javascript">
alert(someName.nodeName);
</script>

Both IE6 and Firefox alert("INPUT"). Why isn''t "someName" undefined?

Why am I not required to do something like this:

getElementById("someName").nodeName;

推荐答案

Bruce写道:
Bruce wrote:

< input id =" someName" type =" text">


< script type =" text / javascript">

alert(someName.nodeName);

< / script>

IE6和Firefox警报(INPUT)。为什么不是someName undefined?


为什么我不需要这样做:


getElementById(" someName")。nodeName;
<input id="someName" type="text">

<script type="text/javascript">
alert(someName.nodeName);
</script>

Both IE6 and Firefox alert("INPUT"). Why isn''t "someName" undefined?

Why am I not required to do something like this:

getElementById("someName").nodeName;



向后兼容性,可能彼此兼容。在Firefox中

它只会因为你通过省略

DOCTYPE声明或提供一个不完整的声明而触发Quirks模式。


< http://validator.w3.org/>

PointedEars

-

Prototype.js由人写的不知道javascript给人们

谁不懂javascript。不知道javascript的人不是设计使用javascript的系统的最佳建议来源。

- Richard Cornford,cljs,< f8 ** *****************@news.demon.co.uk>

Backwards compatibility and maybe compatibility to one another. In Firefox
it would work only because you are triggering Quirks Mode by omitting the
DOCTYPE declaration or providing an incomplete one.

<http://validator.w3.org/>
PointedEars
--
Prototype.js was written by people who don''t know javascript for people
who don''t know javascript. People who don''t know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>


10月2日,2:29 * pm ,Thomas''PointedEars''Lahn< PointedE ... @ web.de>

写道:
On Oct 2, 2:29*pm, Thomas ''PointedEars'' Lahn <PointedE...@web.de>
wrote:

>

向后兼容性,可能彼此兼容。 *在Firefox中

它只会因为您通过省略

DOCTYPE声明或提供不完整的声明而触发Quirks模式。
>
Backwards compatibility and maybe compatibility to one another. *In Firefox
it would work only because you are triggering Quirks Mode by omitting the
DOCTYPE declaration or providing an incomplete one.



是的,你是对的。我忘记了DOCTYPE。这解决了

Firefox的问题,但问题仍然存在于IE6中。

Yes, you''re right. I forgot the DOCTYPE. That fixes the problem for
Firefox but what a nuisance that the problem persists in IE6.


On Thu,2008年10月2日12:45: 19-0700,布鲁斯写道:
On Thu, 02 Oct 2008 12:45:19 -0700, Bruce wrote:

是的,你是对的。我忘记了DOCTYPE。这解决了

Firefox的问题,但这个问题在IE6中仍然存在很麻烦。
Yes, you''re right. I forgot the DOCTYPE. That fixes the problem for
Firefox but what a nuisance that the problem persists in IE6.



IE6,或者更确切地说,迄今为止IE的所有版本都很痛苦。有些浏览器

甚至可以复制IE错误^ w功能。


我的建议:不要污染全局名称空间。


所有类/实例变量都应在本地声明。如果,对于一些

的原因,你需要两个函数来共享一组变量,将两个函数包含在一起




如果你必须使用全局变量,只使用一个。创建一个对象并设置属性。


< html>

< head>

< title>测试< / title>

< / head>

< body>


< script type =" text / javascript" >


var jjsGlobal = {}; //一个变量来统治它们所有


函数w(s)

{

document.write(s +" < br>");

}


(函数(){

var onlySeenHere = 0 ;;


函数test1(){

onlySeenHere ++ ;;

}


函数test2( ){

onlySeenHere ++ ;;

}


test1();

test2();

w(" OnlySeenHere =" + onlySeenHere);


jjsGlobal.test ="测试已设定。" ;;


})();

w(" and our global ==" + jjsGlobal.test);

< / script>


< / body>

< / html>

IE6, or rather, all versions of IE to date, are a pain. Some browsers
even go as far as duplicating IE bugs^w features.

My recommendation: Don''t pollute the global name space.

All class/instance variables should be declared locally. If, for some
reason, you need two functions to share a group of variables, wrap the
two functions up together.

If you MUST use globals, use just one. Make an object and set properties.

<html>
<head>
<title>Test</title>
</head>
<body>

<script type="text/javascript">

var jjsGlobal = {}; // One variable to rule them all

function w(s)
{
document.write(s + "<br>");
}

(function() {
var onlySeenHere = 0;;

function test1() {
onlySeenHere++;;
}

function test2() {
onlySeenHere++;;
}

test1();
test2();
w("OnlySeenHere = " + onlySeenHere);

jjsGlobal.test = "Test is set.";

})();
w("And our global == " + jjsGlobal.test);
</script>

</body>
</html>


这篇关于id名称是全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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