兼容性 [英] Compatibility

查看:68
本文介绍了兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在各种浏览器中,最好使用:

document.getElementById(''num1'')。removeChild(image_ display);



image_display.parentNode.removeChild(image_display);


删除图片。


Ken

Over the various browsers, is it better to use:
document.getElementById(''num1'').removeChild(image_ display);
or
image_display.parentNode.removeChild(image_display );

to remove an image.

Ken

推荐答案

Ken写道:
Ken wrote:
在各种浏览器中,使用它是否更好:
document.getElementById( ''num1'')。removeChild(image_ display);

image_display.parentNode.removeChild(image_display);
Over the various browsers, is it better to use:
document.getElementById(''num1'').removeChild(image_ display);
or
image_display.parentNode.removeChild(image_display );




第二个使用仅IE语法,因为它假设

image_display是一个全局变量,因此单独使用第一个

最佳方法。


但这可能是最好的:


document.getElementById(''image_display'')。parentNod e.removeChild(''image_display'');


第一个o的问题ne是如果在父元素中有任何空格或

,那么图像就不会是第一个

的孩子。
< br $> b $ b -

兰迪

comp.lang.javascript常见问题 - http://jibbering.com/faq


Ken写道:
Over the各种浏览器,使用起来更好:
document.getElementById(''num1'')。removeChild(image_ display);

image_display.parentNode.removeChild(image_display);

删除图像。

Ken
Over the various browsers, is it better to use:
document.getElementById(''num1'').removeChild(image_ display);
or
image_display.parentNode.removeChild(image_display );

to remove an image.

Ken




不要使用圆括号来寻址数组元素(它是一个IE-ism):


document.getElementById [''num1'']。removeChild(image_display);


关于如何引用你要删除的东西,取决于你在做什么
。如果您要删除的东西可以使用

a广泛支持和简单的DOM方法(也许它是一个名为

形式的节点),那么直接传递引用它:


< form ...>

....

< input type ="按钮" value ="删除某些内容"

onclick =" deleteIt(this.form.aNode);">

< / form> ;.


和函数可以非常简单:


函数deleteIt(x){

x.parentNode.remvoeChild.x;

}


但是,有时文档树不会提供简单的解决方案

或元素之间没有可靠的关系点击

和要删除的那个。在这种情况下,您可以传递一个字符串

id并使用getElementById()。如果图像没有id,那么

你可以浏览

getElementsByTagName(''img'')给出的集合,但它有点长啰嗦。


如果使用任何一种getElement(s)方法,你需要做一些功能

测试,以确保用户的浏览器支持它。而不是将

功能测试代码放入运行该功能的东西(比如一个

链接或按钮),你需要将它包含在删除功能中 -

否则你将在每个元素上复制功能测试代码

有一个动作。


已经传递了一些元素的id你知道你可以使用

与要删除的元素建立关系,使用

getElementById(),然后是parentNode:


函数deleteIt(theThing){

if(document.getElementById){

var a = document.getElementById(theThing);

a.parentNode.remvoeChild(a);

} else {

//如果不支持getElementById,可以选择其他方式

}

}


parentNode很好,因为它保证了这种关系。另外,通过从函数中删除任何硬编码引用来获得
,你可以使用它来删除很多东西,而不仅仅是图像。请注意,您可以替换

a;上面有document.getElement .... theThing);在每个例子中,但是

使用变量似乎更整洁。


希望有所帮助! Rob



Don''t use round brackets to address array elements (it is an IE-ism):

document.getElementById[''num1''].removeChild(image_display);

Regarding how to reference the thing you want to delete, it depends on
what you are doing. If the thing you want to delete can be found using
a widely supported and simple DOM method (maybe it''s a named node in a
form), then pass a direct reference to it:

<form...>
....
<input type="button" value="Delete something"
onclick="deleteIt(this.form.aNode);">
</form>.

and the function can be really simple:

function deleteIt(x) {
x.parentNode.remvoeChild.x;
}

However, sometimes the document tree will not provide a simple solution
or there is no reliable relationship between the element being clicked
and the one that is to be deleted. In this case, you can pass a string
id and use getElementById(). If the image does not have an id, then
you can trawl through the collection given by
getElementsByTagName(''img''), but it is somewhat more long winded.

If using either of the getElement(s) methods, you need to do feature
testing to ensure the users'' browser supports it. Rather than put the
feature test code into the thing that is running the function (say a
link or button), you need to include it in the delete function -
otherwise you will be replicating feature test code on every element
that has an action.

Having passed the id of some element that you know you can use to
establish a relationship with the element to be deleted, use
getElementById(), then parentNode :

function deleteIt(theThing) {
if (document.getElementById) {
var a = document.getElementById(theThing);
a.parentNode.remvoeChild(a);
} else {
// some alternative if getElementById not supported
}
}

The parentNode is good because it guarantees the relationship. Also,
by removing any hard coded references from the function, you can use it
to delete lots of things, not just images. Note that you could replace
"a" above with document.getElement....theThing); in each instance, but
it seems neater to use a variable.

Hope that helps! Rob


RobG写道:

[snip]
RobG wrote:
[snip]
function deleteIt(x){
x。 parentNode.remvoeChild.x;
function deleteIt(x) {
x.parentNode.remvoeChild.x;




Oooops,应该是:


x.parentNode.removeChild(x);


Dang租了手指......


Rob。



Oooops, that should be:

x.parentNode.removeChild(x);

Dang rented fingers...

Rob.


这篇关于兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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