[布尔:IE& Gecko] IE认为虚假是真的吗? [英] [boolean: IE & Gecko] IE treats false as true?

查看:49
本文介绍了[布尔:IE& Gecko] IE认为虚假是真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在使用两个代码的sniperts并从中获得非常有趣的结果。


..html

< tr id =" 1"类= QUOT;段" open =" false">


这是'调查'下的细分市场


..js

if(segmentRows [i] .open || segmentRows [i] .open ==''true'')doWhatever();


运行此if语句时(make确保上面的< tr>包含在segmentRows [i]中。)Gecko正确处理它。

因为它*不*输入if。

IE 5+另一方面警告(如在调试中)我将segmentRows [i] .open评估为false,然后*不*

在输入if语句时有任何问题。

我知道这听起来很奇怪,但*实际上正在发生。

再一次,这个完全相同的代码被Gecko *正确处理。


这是一个众所周知的事情,我不应该依赖布尔的东西或......?


任何建议都表示赞赏。


TIA

Fermin DCG

解决方案

F. Da Costa于2003年12月8日在comp.lang.javascript写了

大家好,

以下两个代码片段我使用并获得非常有趣的结果。

.html
< tr id =" 1"类= QUOT;段" open =" false">

这是''调查'下的片段'

.js
if(segmentRows [i] .open || segmentRows [i] .open ==''true'')doWhatever();




< div id =" t1" open = false>

这是''调查'下的分段'

< / div>


< script> ;

z = document.getElementById(" t1");

alert(z.open);


if(z .open)

提醒(存在)

其他

提醒(不存在);


if(z.open!= false)

alert(" boolean value is''false''")

else

alert(" boolean value is''false''");


if(z.open!=" false")

alert(" string value not''false''")

else

alert(" string value is''false'' ");

< / script>


我建议你单独测试segmentRows [i]

-

Evertjan。

荷兰。

(请将x''es更改为我的电子邮件地址中的点数)





F. Da Costa写道:

跟随两个代码的sniperts我正在使用并且变得非常有趣
结果来自。

.html
< tr id =" 1"类= QUOT;段"开放= QUOT假QUOT;>


您在哪个HTML版本中找到名为open的属性< tr>

元素?

是任何浏览器具有< tr>的对象模型元素对象

名为open的属性是否存在?如果是这样,对象模型告诉

关于属性的类型?


您所做的是将自己的属性open添加到HTML< ; tr>

元素。该属性是否作为属性反映在DOM中

取决于浏览器,IE / Win至少已知添加属性,

Mozilla已知不做那。所以用IE

trElement.open

有一个值,而Mozilla

trElement.open

未定义。


例如尝试以下方法:


< html>

< head>

< title>自定义属性和脚本< / title>

< script type =" text / javascript">

function checkAttribute(element, attributeName){

if(!element){

return;

}

else {

var result ='''';

var attributeValue = element.getAttribute(attributeName);

result + =''element.getAttribute("' '+ attributeName +''"):''

+ element.getAttribute(attributeName);

var propertyType = typeof element [attributeName];

结果+ =''\ n'';

结果+ =''typeof element ["''+ attributeName +''"]:''+(typeof

元素[attributeNam e]);

提醒(结果);

}

}

window.onload = function(evt) ){

if(document.getElementById){

checkAttribute(document.getElementById(''a'''),''open'');

}

};

< / script>

< / head>

<身体>

< p id =" aP" open =" false">



< / p>

< / body>

< / html>


使用IE6 / Win,您将看到名为open的属性已定义但是

类型字符串,因此您的主题行抱怨booleans简单

并没有描述代码中发生了什么。

使用Mozilla,你会发现名为open的属性是未定义的,所以

你所有访问elementVariable.open的尝试都只是在Mozilla中产生未定义的

,而且代码中发生的事情与

布尔值无关。

使用Opera 7,您还会发现该属性未定义。

因此,如果您使用自定义属性并希望进行跨浏览器脚本编写

那么

element.attributename

没有帮助,使用

element.getAttribute(''attributename'')

代替并注意这总是返回一个字符串值。

这是''调查'下的部分'

.js
if(segmentRows [i] .open || segmentRows [i] .open ==''true'')doWhatever();

运行此if语句时(确保在segmentRows [i]中包含上面的< tr>
])Gecko正确处理这个问题。


好​​吧,Mozilla是第一个条件

segmentRows [i] .open

产生undefined,转换为布尔值产生错误所以

第二个条件

segmentRows [i] .open ==''true''

被评估

''undefined''==''true

是假的,所以整个条件都是假的。

因为它*不*输入if。<另一方面,IE 5+警告(如在调试中)我将segmentRows [i] .open
评估为false,然后*不*在输入if语句时没有任何问题。




使用IE第一个条件

segmentRows [i] .open

产生''false'',转换为布尔值,任何非空的
字符串都会产生布尔值true,因此整个或条件为真。


不是你想要发生的事情但是完全正确。


自定义属性值得怀疑但是如果你使用的话然后学习如何用
编写脚本。

-


Martin Honnen
http://JavaScript.FAQTs.com/

Evertjan。写道:

F. Da Costa于2003年12月8日在comp.lang.javascript中写道

大家好,

以下两个代码我正在使用并获得非常有趣的结果。

.html
< ; tr id =" 1"类= QUOT;段" open =" false">

这是''调查'下的片段'

.js
if(segmentRows [i] .open || segmentRows [i] .open ==''true'')doWhatever();

< div id =" t1" open = false>
这是''调查'下的片段'
< / div>

< script>
z = document.getElementById(" ; t1");
alert(z.open);

if(z.open)
alert(存在)


警告(不存在);

if(z.open!= false)
警告("布尔值不是''false''")
else
alert(" boolean value is''false''");



将上面的行更改为

if(z。 open == false)alert(" boolean value is''false''")

else alert(" boolean value is not''false''");

if(z.open!=" false")
alert(" string value not''false''")

alert(" string)值是''false''");
< / script>

我建议您单独测试segmentRows [i]



测试你的鼻子在IE 5+上使用ip(使用修改的行)并得到以下输出:

false - exists - *布尔值不是''false''* - 字符串值是''false''

你可以看到第三个结果是*不是*人们期望的。

Gecko让我:

undefined - 不存在 - *布尔值不是''false''* - 字符串值不是''false''

我对不存在的性质有点惊讶,但是否则可以说这是正确的。


您是否以其他方式进行测试,因为我仍然没有得到z.open == false *而不是*结束于当时而不是其他。


Hi all,

Following two sniperts of code I''m using and getting very interesting results from.

..html
<tr id="1" class="segment" open="false">

This is the segment under ''investigation''

..js
if (segmentRows[i].open || segmentRows[i].open==''true'') doWhatever();

When running this if statement (made sure that above <tr> is contained in segmentRows[i]) Gecko processes this properly.
As in it *does not* enter the if.
IE 5+ on the other hand alerts (as in debug) me that segmentRows[i].open evaluates to false and subsequently does *not*
have any trouble entering the if statement.

I know this sounds strange but it *is* actually taking place.
Once again this exact same code is *processed correctly by Gecko*.

Is this a known thing, should I not rely on boolean stuff or ...?

Any suggestions are appreciated.

TIA
Fermin DCG

解决方案

F. Da Costa wrote on 08 dec 2003 in comp.lang.javascript:

Hi all,

Following two sniperts of code I''m using and getting very interesting
results from.

.html
<tr id="1" class="segment" open="false">

This is the segment under ''investigation''

.js
if (segmentRows[i].open || segmentRows[i].open==''true'') doWhatever();



<div id="t1" open=false>
This is the segment under ''investigation''
</div>

<script>
z=document.getElementById("t1");
alert(z.open);

if (z.open)
alert("exists")
else
alert("does not exist");

if (z.open!=false)
alert("boolean value is not ''false''")
else
alert("boolean value is ''false''");

if(z.open!="false")
alert("string value is not ''false''")
else
alert("string value is ''false''");
</script>

I suggest that you test the segmentRows[i] seperately

--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)




F. Da Costa wrote:

Following two sniperts of code I''m using and getting very interesting
results from.

.html
<tr id="1" class="segment" open="false">
In which HTML version do you find the attribute named open for <tr>
elements?
Is the any browser having an object model for <tr> element objects where
a property named open exists? If so what does the object model tell
about the type of the property?

What you have done is added your own attribute named open to a HTML <tr>
element. Whether that attribute is reflected in the DOM as a property
depends on the browser, IE/Win at least is known to add a property,
Mozilla is known not to do that. So with IE
trElement.open
has a value while with Mozilla
trElement.open
is undefined.

For instance try the following:

<html>
<head>
<title>custom attributes and scripting</title>
<script type="text/javascript">
function checkAttribute (element, attributeName) {
if (!element) {
return;
}
else {
var result = '''';
var attributeValue = element.getAttribute(attributeName);
result += ''element.getAttribute("'' + attributeName + ''"): ''
+ element.getAttribute(attributeName);
var propertyType = typeof element[attributeName];
result += ''\n'';
result += ''typeof element["'' + attributeName + ''"]: '' + (typeof
element[attributeName]);
alert(result);
}
}
window.onload = function (evt) {
if (document.getElementById) {
checkAttribute(document.getElementById(''aP''), ''open'');
}
};
</script>
</head>
<body>
<p id="aP" open="false">
paragraph
</p>
</body>
</html>

With IE6/Win you will see that the property named open is defined but of
type string so your subject line complaining about booleans simple
doesn''t describe what is happening in the code.
With Mozilla you will seee that the property named open is undefined so
all your attempts to access elementVariable.open simply yield undefined
in Mozilla and again what is happening in code has nothing to do with
boolean values.
With Opera 7 you will also find that the property is undefined.
Thus if you use custom attributes and want to do cross-browser scripting
then
element.attributename
doesn''t help, use
element.getAttribute(''attributename'')
instead and be aware that that always returns a string value.

This is the segment under ''investigation''

.js
if (segmentRows[i].open || segmentRows[i].open==''true'') doWhatever();

When running this if statement (made sure that above <tr> is contained
in segmentRows[i]) Gecko processes this properly.
Ok, with Mozilla the first condition
segmentRows[i].open
yields undefined, is converted to a boolean which yields false so the
second condition
segmentRows[i].open==''true''
is evaluated
''undefined'' == ''true
is false so the whole condition is false.
As in it *does not* enter the if.
IE 5+ on the other hand alerts (as in debug) me that segmentRows[i].open
evaluates to false and subsequently does *not* have any trouble entering
the if statement.



With IE the first condition
segmentRows[i].open
yields ''false'', that is converted to a boolean value and any non empty
string yields the boolean value true so the whole or condition is true.

Not what you want to happen but perfectly correct.

Custom attributes are questionable but if you use them then learn how to
script them.
--

Martin Honnen
http://JavaScript.FAQTs.com/


Evertjan. wrote:

F. Da Costa wrote on 08 dec 2003 in comp.lang.javascript:

Hi all,

Following two sniperts of code I''m using and getting very interesting
results from.

.html
<tr id="1" class="segment" open="false">

This is the segment under ''investigation''

.js
if (segmentRows[i].open || segmentRows[i].open==''true'') doWhatever();
<div id="t1" open=false>
This is the segment under ''investigation''
</div>

<script>
z=document.getElementById("t1");
alert(z.open);

if (z.open)
alert("exists")
else
alert("does not exist");

if (z.open!=false)
alert("boolean value is not ''false''")
else
alert("boolean value is ''false''");


changed line above to
if (z.open==false) alert("boolean value is ''false''")
else alert("boolean value is NOT ''false''");

if(z.open!="false")
alert("string value is not ''false''")
else
alert("string value is ''false''");
</script>

I suggest that you test the segmentRows[i] seperately


Tested your snip on IE 5+ (with the modded line) and got the following output:
false - exists - *boolean value is NOT ''false''* - string value is ''false''
As you can see the third result is *not* what one would expect.
Gecko got me:
undefined - does not exist - *boolean value is NOT ''false''* - string value is not ''false''
I am a bit surprised about the non-existence nature but otherwise one can argue this is all correct.

Did you test in some other way because I still don''t get the z.open==false *not* ending up in the then instead of the else.


这篇关于[布尔:IE&amp; Gecko] IE认为虚假是真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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