使用对它的引用调用DOM方法时的异常 [英] Exception when calling a DOM method using a reference to it

查看:68
本文介绍了使用对它的引用调用DOM方法时的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了回答这个小组中的另一个问题,我不得不求助于

来调用DOM方法Node.removeChild(),使用它来引用它

(长话故事......)。也就是说,传递Node.removeChild。


在Opera(7.23 / Win)中,调用似乎什么都不做 - 节点仍然是 -

但没有错误显示。在Netscape(7.0 / Win)中,异常结果为[1]。

在IE(6.0 / Win)上,节点被删除。


Strangly,如果我传递另一个函数引用,例如window.alert或

用户定义的函数,则没有问题。


这说明了问题:


<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 // EN"

" http://www.w3.org /TR/html4/strict.dtd">


< html>

< head>

< meta HTTP的当量= QUOT;内容类型"含量=" text / html的; charset = iso-8859-1">

< meta http-equiv =" Content-Script-Type" content =" text / javascript">


< title>函数参考测试< / title>


< script type = " text / javascript">

函数executeRef(func,arg){

func(arg);

}


函数performRefTest(){

var elem = document.getElementById(''testDiv'');

executeRef(elem.parentNode.removeChild ,elem);

}

< / script>

< / head>


< body onload =" performRefTest()">

< div id =" testDiv">

页面内容

< / div>

< div>

这应该是剩下的唯一文字。

< / div>

< / body>

< / html>


有什么建议吗?


Mike

[1]消息:


错误:未捕获异常:[例外......非法操作

WrappedNati原型对象 nsresult:" 0x8057000c

(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location:JS frame ::

file:/// F:/Web/Blockquote/cite.js :: timerInstance_fire :: line 103"

data:no ]


-

Michael Winter
M。****** @ blueyonder.co.inva 盖子(将.invalid替换为.uk以回复)

In an attempt to answer another question in this group, I''ve had to resort
to calling the DOM method, Node.removeChild(), using a reference to it
(long story...). That is, passing Node.removeChild.

In Opera (7.23/Win), the call appears to do nothing - the node remains -
but no errors are shown. In Netscape (7.0/Win), an exception results[1].
On IE (6.0/Win), the node is removed.

Strangly, if I pass another function reference, say window.alert or a
user-defined function, there is no problem.

This illustrates the problem:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title>Function reference test</title>

<script type="text/javascript">
function executeRef( func, arg ) {
func( arg );
}

function performRefTest() {
var elem = document.getElementById(''testDiv'');
executeRef( elem.parentNode.removeChild, elem );
}
</script>
</head>

<body onload="performRefTest()">
<div id="testDiv">
Page content
</div>
<div>
This should be the only text remaining.
</div>
</body>
</html>

Any suggestions?

Mike
[1] The message:

Error: uncaught exception: [Exception... "Illegal operation on
WrappedNative prototype object" nsresult: "0x8057000c
(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame ::
file:///F:/Web/Blockquote/cite.js :: timerInstance_fire :: line 103"
data: no]

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)

推荐答案

2004年2月14日星期六20:18:07 GMT,Michael Winter

< M。****** @ blueyonder.co.invalid>写道:


[snip]
On Sat, 14 Feb 2004 20:18:07 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:

[snip]
在Netscape(7.0 / Win)中,异常结果为[1]。


[snip]

[1]消息:

错误:未捕获的异常:[例外......非法对WrappedNative原型对象的操作 nsresult:" 0x8057000c
(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location:" JS frame ::
file:/// F:/Web/Blockquote/cite.js :: timerInstance_fire :: line 103"
data:no]
In Netscape (7.0/Win), an exception results[1].
[snip]
[1] The message:

Error: uncaught exception: [Exception... "Illegal operation on
WrappedNative prototype object" nsresult: "0x8057000c
(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame ::
file:///F:/Web/Blockquote/cite.js :: timerInstance_fire :: line 103"
data: no]




这实际上是错误的信息,而不是它有任何区别。

然而,为了正确:


错误:未捕获的异常:[例外...非法操作

WrappedNative原型对象 nsresult:" 0x8057000c

(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location:" JS frame ::

file:/// F:/Web/test.html :: executeRef :: line 13"数据:没有]


上一条消息来自我正在处理的实际脚本,而

上面的消息来自我发布的样本。


迈克


-

Michael Winter
M。****** @ blueyonder.co.inva 盖子(将.invalid替换为.uk以回复)



That''s actually the wrong message, not that it makes any difference.
However, for correctness:

Error: uncaught exception: [Exception... "Illegal operation on
WrappedNative prototype object" nsresult: "0x8057000c
(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame ::
file:///F:/Web/test.html :: executeRef :: line 13" data: no]

The previous message was from the actual script I was working on, whereas
the one above is from the sample I posted.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)


" Michael Winter" < M ****** @ blueyonder.co.invalid>。在留言中写道

news:op ************** @ news-text.blueyonder.co.uk ...

< ; snip>
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
<snip>
< script type =" text / javascript">
function executeRef(func,arg){
func(arg);
}

函数performRefTest(){
var elem = document.getElementById(''testDiv'');
executeRef(elem.parentNode.removeChild,elem);
}
< / script>
< / head>
<script type="text/javascript">
function executeRef( func, arg ) {
func( arg );
}

function performRefTest() {
var elem = document.getElementById(''testDiv'');
executeRef( elem.parentNode.removeChild, elem );
}
</script>
</head>



< snip>


你正在路过对 - executeRef -

函数的节点方法的引用。一旦该函数接收到引用,它只是函数对象的引用

而不是对象的方法。与 -

elm.parentNode - 的联系已经消失。


对于本机代码函数,它不一定完全相同

(因此它使用IE)但调用--func(arg); - 对于javascript

方法将在全局范围内执行它并导致 - this -

关键字引用窗口对象而不是 - elm.parentNode - 。

和 - arg - 不是全局对象的子节点,因此节点不能被删除,假设该函数在该上下文中运行而没有

错误(就像在Mozilla上一样)


Richard。


<snip>

You are passing a reference to the method of a node to the - executeRef -
function. Once that function receives the reference it is just a reference
to a function object and not a method of an object. The association with -
elm.parentNode - is gone.

It would not necessarily work exactly the same for native code functions
(hence it working with IE) but calling - func(arg); - for a javascript
method would execute it in the global scope and result in the - this -
keyword referring to the window object instead of - elm.parentNode - .
And - arg - is not a child of the global object so that node could not be
removed, assuming the function would operate in that context at all without
erroring (as it does on Mozilla)

Richard.


2004年2月14日星期六20:18 :07 GMT,迈克尔·温特写道:
On Sat, 14 Feb 2004 20:18:07 GMT, Michael Winter wrote:
在Opera(7.23 / Win)中,调用似乎什么都不做 - 节点仍然存在 -
但没有显示错误。在Netscape(7.0 / Win)中,异常结果为[1]。
在IE(6.0 / Win)上,节点被删除。
In Opera (7.23/Win), the call appears to do nothing - the node remains -
but no errors are shown. In Netscape (7.0/Win), an exception results[1].
On IE (6.0/Win), the node is removed.




可能' '因为你只引用原型功能。


我的意思是:


< script type =" text / javascript">

myobject = {

消息:" default",

me:function(){

alert(this .message);

}

}


myobject.me();


var x = myobject.me;

x();

< / script>


你怎么看, x变量point功能,而不是整个对象。


如果函数调用object,你会得到一个异常,用原生的

代码。


一个可能的解决方法可能是:


函数executeRef(func,arg,obj){

obj = obj || window ;

obj [func](arg);

}

函数performRefTest(){


executeRef(" alert",this is a argument);

var elem = document.getElementById(''testDiv'');

executeRef(" ; removeChild",elem,elem.parentNode);

}


但也许这不适合你。


-

C''ya,

ZER0 :: coder.gfxer.webDesigner();


当你消除了不可能的东西,剩下的东西,

然而不可能,必须是真理。 (S.H.)



Probably ''cause you referer only the prototype function.

I mean:

<script type="text/javascript">
myobject={
message:"default",
me:function(){
alert(this.message);
}
}

myobject.me();

var x=myobject.me;
x();
</script>

How you can see, the x variable "point" to function, not the entire object.

If the function make a call to object, you get an exception, in native
code.

A possible workaround could be:

function executeRef(func, arg, obj) {
obj=obj||window;
obj[func](arg);
}
function performRefTest() {

executeRef ("alert","this is an argument");
var elem = document.getElementById(''testDiv'');
executeRef ("removeChild",elem, elem.parentNode);
}

But maybe this is not a solution for you.

--
C''ya,
ZER0 :: coder.gfxer.webDesigner();

"When you have eliminated the impossible, whatever remains,
however improbable, must be the truth." (S.H.)


这篇关于使用对它的引用调用DOM方法时的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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