删除window.opener中的节点 [英] removing nodes in the window.opener

查看:59
本文介绍了删除window.opener中的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下功能应该删除名称为

且以keywords开头的所有子节点。在myform中在window.opener中。


如果有一个关键字节点,它可以正常工作。但如果有很多关键字节点,你必须多次运行

函数。

为什么?


函数removeKeywords()

{

var form_obj = window.opener.document.getElementById(''myform'');

var num_of_elem = form_obj.children。长度;

var count;

for(count = 0; count< num_of_elem; count ++)

{

var name = form_obj.children [count] .name;

if(name.match(/ ^ keywords。* /))

{

form_obj.children [count] .removeNode();

}


}

}

The function below is supposed to remove all childnodes with a name
that starts with "keywords" in "myform" in the window.opener.

It works fine if there''s one keyword node. But you have to run the
function several times if there are many keyword nodes.
Why?

function removeKeywords()
{
var form_obj = window.opener.document.getElementById(''myform'');
var num_of_elem = form_obj.children.length;
var count;
for (count = 0; count<num_of_elem;count++)
{
var name = form_obj.children[count].name;
if (name.match(/^keywords.*/))
{
form_obj.children[count].removeNode();
}

}
}

推荐答案



" Ole Noerskov"写了

"Ole Noerskov" wrote
下面的函数应该删除所有名称为
且以keywords开头的子节点。在myform中在window.opener中。
如果有一个关键字节点,它可以正常工作。但是如果有很多关键字节点,你必须多次运行
函数。
为什么?


因为表格元素数组缩短了,并且当你删除元素时,任何剩余的元素都会被更新(变少一些)。如果

你删除16号元素,则数字17变为16,所以你想再次检查16



函数removeKeywords()
{
var form_obj = window.opener.document.getElementById(''myform'');
var num_of_elem = form_obj.children.length;
var count;
for (count = 0; count< num_of_elem; count ++)
{
var name = form_obj.children [count] .name;
if(name.match(/ ^ keywords。* /))
{form /obj.children [count] .removeNode();


count--; //在这里添加这一行

}
}
}
The function below is supposed to remove all childnodes with a name
that starts with "keywords" in "myform" in the window.opener.
It works fine if there''s one keyword node. But you have to run the
function several times if there are many keyword nodes.
Why?
Because the form elements array is shortened and any remaining elements have
their index numbers updated (become one less) when you remove an element. If
you remove element number 16, number 17 becomes 16, so you want to check 16
again.

function removeKeywords()
{
var form_obj = window.opener.document.getElementById(''myform'');
var num_of_elem = form_obj.children.length;
var count;
for (count = 0; count<num_of_elem;count++)
{
var name = form_obj.children[count].name;
if (name.match(/^keywords.*/))
{
form_obj.children[count].removeNode();
count--; // add this line here
}
}
}




HTH

Ivo



HTH
Ivo


Ivo写道:
Ivo wrote:
" Ole Noerskov"写了
"Ole Noerskov" wrote
function removeKeywords()
{var /> var form_obj = window.opener.document.getElementById(''myform'');
var num_of_elem = form_obj.children 。长度;


该属性为childNodes,而不是children。 孩子

可能在IE中有效,但它不符合标准。要支持旧的IE,你可以写一下


var children = form_obj.childNodes || form_obj.children;

if(children)

{

var num_of_elem = children.length;

.. 。

}


但是你还需要


var formObj = window.opener.document.forms [ ''myform''];


以前,因为低于5.0的IE不支持

W3C DOM,只支持DOM Level 0和IE4 DOM,如果那样。

var count;


这是不必要的,除非你打算在函数中再次使用`num_of_elem''

和`count''。否则写(
$ b; count< num_of_elem; count ++)


for(var count = 0,num_of_elem = form_obj.childNodes.length;

count< num_of_elem;

count ++)


代替。

{
var name = form_obj.children [count] .name;


这不好。你在每个循环中重新声明变量。

我建议在`for''语句的初始化部分声明一个包含对象引用的变量




for(var count = 0,e = form_obj.childNodes,

num_of_elem = form_obj.childNodes.length,

sName ="" ;; //见下文

count< num_of_elem;

count ++)


if(name.match(/ ^ keywords) 。* /))


因为你似乎没有评估匹配,RegExp.test()就足够了

它返回一个布尔值,避免支付类型转换。

{
form_obj.children [count] .removeNode();
function removeKeywords()
{
var form_obj = window.opener.document.getElementById(''myform'');
var num_of_elem = form_obj.children.length;
The property is "childNodes", not "children". "children"
may work in IE, but it is not standards compliant. To
support older IEs, you could write

var children = form_obj.childNodes || form_obj.children;
if (children)
{
var num_of_elem = children.length;
...
}

But then you also need

var formObj = window.opener.document.forms[''myform''];

instead previously, as IEs below 5.0 do not support the
W3C DOM, only DOM Level 0 and the IE4 DOM, if that.
var count;
That is unnecessary, unless you intend to use `num_of_elem''
and `count'' again in the function. Otherwise write
for (count = 0; count<num_of_elem;count++)
for (var count = 0, num_of_elem = form_obj.childNodes.length;
count < num_of_elem;
count++)

instead.
{
var name = form_obj.children[count].name;
That is not good. You redeclare the variable in every loop.
I recommend to declare a variable holding the object reference
in the initialization part of the `for'' statement:

for (var count = 0, e = form_obj.childNodes,
num_of_elem = form_obj.childNodes.length,
sName = ""; // see below
count < num_of_elem;
count++)

if (name.match(/^keywords.*/))
Since you seem not evaluate the matches, RegExp.test() is sufficient
and it returns a boolean value which avoids paying for type casting.
{
form_obj.children[count].removeNode();



count--; //在这里添加这一行



count--; // add this line here




考虑到上述情况,写下


if((sName = e [count] .name )

&& /^keywords.*/.test(sName))

{

form_obj.removeChild(e [count- - ]);

}


代替。 AFAIS removeNode()是专有的[1](在W3C DOM Level 2

规范的Document接口规范中显然有一个

错误,因为Node接口没有这种方法可以继承[b]继承[b]。它已经不再存在于W3C DOM Level 3 Core中。[3])

PointedEars

___________

[1]

< http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/removenode.asp>

[2]< http://www.w3.org/TR/DOM-Level-2-Core/core.html#i-Document>

[3]< ; http://www.w3.org/TR/DOM-Level-3-Core/core.html#i-Document>



Considering the above, write

if ((sName = e[count].name)
&& /^keywords.*/.test(sName))
{
form_obj.removeChild(e[count--]);
}

instead. AFAIS removeNode() is proprietary[1] (there is apparently a
typo in the Document interface specification of the W3C DOM Level 2
Specification, as the Node interface has no such method that could be
inherited[2]. It is no longer there in W3C DOM Level 3 Core.[3])
PointedEars
___________
[1]
<http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/removenode.asp>
[2] <http://www.w3.org/TR/DOM-Level-2-Core/core.html#i-Document>
[3] <http://www.w3.org/TR/DOM-Level-3-Core/core.html#i-Document>


托马斯''PointedEars'' Lahn写道:
Thomas ''PointedEars'' Lahn wrote:
Ivo写道:
" Ole Noerskov"写了
< snip>
"Ole Noerskov" wrote <snip>
{
var name = form_obj.children [count] .name;
{
var name = form_obj.children[count].name;


这不好。你在每个循环中重新声明变量。


That is not good. You redeclare the variable in every loop.




因为ECMAScript不是块作用域并且为函数执行变量

实例化,因为它进入一个执行上下文,它说在每个循环中重新声明变量是不正确的。

局部变量在一个循环中的代码中声明,但是该声明

是在函数级别处理的,只发生一次,在
$ b之前$ b执行函数体。

在变量实例化之后,当函数执行时,该语句唯一的

重要性是作为赋值,是什么是

想要的。 -var-keyword已被用于创建执行上下文的Variable对象的

属性,其名称为name

,该进程不会无论使用-var-keyword的(或任何)
语句多久经常被执行,都要重复。

我建议申报在`for''语句的初始化部分中保存对象引用的变量:



Because ECMAScript is not block scoped and performs variable
instantiation for a function once, as it enters an execution context, it
would not be correct to say "redeclare the variable in every loop". The
local variable is declared in code within a loop, but that declaration
is handled at a function level and only happens once, prior to the
execution of the function body.

Following variable instantiation, as the function is executing, the only
significance of that statement is as an assignment, which is what is
wanted. The - var - keyword has already been employed to create a
property of the execution context''s Variable object with the name "name"
and that process will not be repeated no matter how often the (or any)
statement(s) in which the - var - keyword is used is(are) executed.
I recommend to declare a variable holding the object reference
in the initialization part of the `for'' statement:



< snip>


好的风格可能会建议变量在函数定义的顶部

中的块中声明,或者在 - for -

语句的初始化部分中声明,但是位置变量声明与ECMAScript的执行方式没有实际的区别。这意味着如果应用了

紧凑代码的标准,而不是概念正确的样式,则

首先在第一个语句中明确声明一个局部变量

对它进行了分配可以节省几个字节的下载而不会使代码执行方式与

有任何区别。


理查德。


<snip>

Good style may suggest that variables be declared in a block at the top
of a function definition, or in the initialisation part of a - for -
statement, but the location of variable declarations makes no actual
difference to how ECMAScript executes. Which means that if a criteria of
compact code was applied, instead of a notion "correct style", then
first explicitly declaring a local variable in the first statement that
made an assignment to it may save a few bytes of download without making
any difference to how the code executed.

Richard.


这篇关于删除window.opener中的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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