typeof.currentElement.innerText在Firefox中不起作用? [英] typeof.currentElement.innerText not working in Firefox?

查看:84
本文介绍了typeof.currentElement.innerText在Firefox中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在firefox中测试以下代码

函数fDHTMLPopulateFields(displayValuesArray,displayOrderArray)

{

var i,

currentElement,

displayFieldID,

currentChild,
nDisplayValues = displayValuesArray.length;

for(i = 0; i< nDisplayValues; i ++){

displayFieldID =" div_" + displayOrderArray [i] +" ID";

currentElement =(document.getElementById

&& document.getElementById(displayFieldID))

|| (document.all

&& document.all(displayFieldID));

if(typeof currentElement ==" object"){

if(typeof currentElement.innerText!=" undefined"){

currentElement.innerText = displayValuesArray [i];

}

否则如果(

currentElement.firstChild

&& currentElement.removeChild

&& currentElement.appendChild

&& document.createTextNode

){

while((currentChild = currentElement.firstChild)){

currentElement.removeChild( currentChild);

}

currentElement.appendChild(

document.createTextNode(displayValuesArray [i])

) ;

}

}

}

}


代码在IE中运行但在FF中运行它不会进入循环

--->

if(typeof currentElement.innerText!=" undefined")


如果这样的话g只有IE识别?我是否将其更改为在FF中工作?


提前致谢。

解决方案


ef*****@epitome.com.sg 写道:


我在firefox中测试以下代码



[...]


代码在IE中运行但在FF中运行它不会进入循环

--->

if(typeof currentElement.innerText!= 未定义)


如果这个东西只有IE识别?我是否将其更改为在FF中工作?



innerText是一个专有的IE属性,W3C等价物(由Firefox支持

)是textContent。您可能希望使用

在各种浏览器中运行的功能:


function getText(el)

{

if(''string''== typeof el.textContent)return el.textContent;

if(''string''== typeof el.innerText)return el.innerText;

返回el.innerHTML.replace(/< [^>] *> / g,'''');

}


其中''el''是对DOM元素的引用。

-

Rob


RobG写道:


function getText(el)

{

if(''string''== typeof el.textContent)return el.textContent;

if(''string''== typeof el.innerText)return el.innerText;

返回el.innerHTML.replace(/< [^>] *> / g,'''');

}


这个完全偏离主题的是,这是由Yoda编程的吗?


我知道他们的订单并不重要,但我认为


if(typeof el.textContent ==''string'' )返回el.textContent;


会更好读。 if(5 == x)对我来说似乎是倒退了。


-

Trond Michelsen


Trond Michelsen写道:


RobG写道:


function getText(el)

{

if(''string''== typeof el.textContent)return el.textContent;

if(''string''== typeof el.innerText)return el .innerText;

返回el.innerHTML.replace(/< [^>] *> / g,'''');

}



这个完全偏离主题的是,



这不是主题。该小组的主题是javascript以及如何编写j b代码可以写成javascript代码肯定是主题。不要让

帖子的主题欺骗你,不要将随后的讨论限制在任何特定的事情上(除非是巧合)。
< blockquote class =post_quotes>
但这是由Yoda编程的吗?


我知道他们的订单并不重要,但我只是认为


if(typeof el.textContent ==''string'')返回el.textContent;


会更好读。 if(5 == x)对我来说似乎是如此倒退。



以这种方式编写比较避免了使用

和赋值运算符代替比较运算符的常见错误。如果

代码尝试分配给字符串(或实际上任何)字面值,那么

错误就会发生。将字符串值分配给

标识符是合法的,并且相应的错误(如果发现的话)

在其他地方发生,可能很难归因于它们的原因。 br />

优先级在此中扮演一个角色,因为赋值具有更高的优先级

而不是 - typeof - 其优先级高于比较优先级。所以: -


typeof x ==''string''


-is: -


((typeof x)==''string'')


- 同时: -


typeof x =''string' '


-is: -


(typeof(x =''string''))//这不可避免地是布尔值true

//如果在 - if - 表达式中使用。


Richard。


I am testting the following code in firefox

function fDHTMLPopulateFields(displayValuesArray, displayOrderArray)
{
var i,
currentElement,
displayFieldID,
currentChild,
nDisplayValues = displayValuesArray.length;
for (i=0; i<nDisplayValues; i++) {
displayFieldID = "div_" + displayOrderArray[i] + "ID";
currentElement = (document.getElementById
&& document.getElementById(displayFieldID))
|| (document.all
&& document.all(displayFieldID));
if (typeof currentElement == "object") {
if (typeof currentElement.innerText != "undefined") {
currentElement.innerText = displayValuesArray[i];
}
else if (
currentElement.firstChild
&& currentElement.removeChild
&& currentElement.appendChild
&& document.createTextNode
) {
while ((currentChild = currentElement.firstChild)) {
currentElement.removeChild(currentChild);
}
currentElement.appendChild(
document.createTextNode(displayValuesArray[i])
);
}
}
}
}

The code works in IE but running in FF it does not go into the loop
--->
if (typeof currentElement.innerText != "undefined")

if this something only IE recognises? I do I change it to work in FF?

Thanks in advance.

解决方案


ef*****@epitome.com.sg wrote:

I am testting the following code in firefox

[...]

The code works in IE but running in FF it does not go into the loop
--->
if (typeof currentElement.innerText != "undefined")

if this something only IE recognises? I do I change it to work in FF?

innerText is a proprietary IE property, the W3C equivalent (supported
by Firefox) is textContent. You might like to use a function that
works in a wide variety of browsers:

function getText(el)
{
if (''string'' == typeof el.textContent) return el.textContent;
if (''string'' == typeof el.innerText) return el.innerText;
return el.innerHTML.replace(/<[^>]*>/g,'''');
}

where ''el'' is a reference to a DOM element.
--
Rob


RobG wrote:

function getText(el)
{
if (''string'' == typeof el.textContent) return el.textContent;
if (''string'' == typeof el.innerText) return el.innerText;
return el.innerHTML.replace(/<[^>]*>/g,'''');
}

This completely off-topic is, but programmed this was by Yoda?

I know their order doesn''t really matter, but I would just think that

if (typeof el.textContent == ''string'') return el.textContent;

would read better. "if (5 == x)" just seems so backwards to me.

--
Trond Michelsen


Trond Michelsen wrote:

RobG wrote:

function getText(el)
{
if (''string'' == typeof el.textContent) return el.textContent;
if (''string'' == typeof el.innerText) return el.innerText;
return el.innerHTML.replace(/<[^>]*>/g,'''');
}


This completely off-topic is,

It is not off topic. The topic of the group is javascript and how
javascript code may be written is certainly on topic. Don''t let the
subjects of posts fool you, the do not restrict the subsequent
discussion to anything in particular (except by coincidence).

but programmed this was by Yoda?

I know their order doesn''t really matter, but I would just think that

if (typeof el.textContent == ''string'') return el.textContent;

would read better. "if (5 == x)" just seems so backwards to me.

Writing the comparison this way around avoids the common error where
and assignment operator is used in place of a comparisons operator. If
code attempts to assign to a string (or indeed any) literal then the
error happens at that point. While assigning a string value to an
Identifier is legal, and the consequential errors (if spotted at all)
happen elsewhere and may be difficult to attribute to their cause.

Precedence plays a role in this as assignment has higher precedence
than - typeof - which has higher precedence than comparison. So:-

typeof x == ''string''

-is:-

((typeof x) == ''string'')

- while:-

typeof x = ''string''

-is:-

(typeof (x = ''string'')) //which is inevitably boolean true
// if used in an - if - expression.

Richard.


这篇关于typeof.currentElement.innerText在Firefox中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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