直接使用setAttribute或set属性 [英] using setAttribute or set attribute directly

查看:92
本文介绍了直接使用setAttribute或set属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇是否有直接设置属性的好处,在我的
javascript中,或者使用setAttribute。


例如,我有这个:

var input = document.createElementNS(xhtmlNS,''input'');

input.setAttribute(''width'',''20em'') ;


我原本可以调用input.width ='''20em''


什么时候更好用,或者没有它们之间有什么区别?


另外,以这种方式使用setAttribute是不合适的:

input.setAttribute(''onchange'',mychangefunction)


感谢您的帮助。

解决方案

James Black写道:

另外,以这种方式使用setAttribute是不合适的:
input.setAttribute(''onchange'',mychangefunction)



你试过吗?我没有。


即使它确实有效,也会模糊不清,只需直接分配事件处理程序



-

Ian Collins。


James Black在2006年5月20日下午9:40发表以下内容:< blockquote class =post_quotes>我很好奇是否有直接设置属性的好处,在我的
javascript中,或者使用setAttribute。


是的,直接设置属性是有好处的。 setAttribute

已知在IE中有问题,所以为什么还要烦扰代码?

例如,我有这个:
var input = document.createElementNS (xhtmlNS,''input'');
input.setAttribute(''width'',''20em'');

我本来可以调用input.width ='' 20em''


这是一种更好的方法。

什么时候更好用,或者它们之间没有区别?


不同之处在于IE中有一些setAttribute调用的错误行为。

此外,以这种方式使用setAttribute是不合适的:
输入.setAttribute(''onchange'',mychangefunction)




不知道,从未使用过它。 input.onchange = mychangefunction;


-

兰迪

comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /


" James Black" < PL *********** @ gmail.com>写道:

我很好奇是否有直接设置属性的好处,在我的
javascript中,或者使用setAttribute。

例如,我有这个:
var input = document.createElementNS(xhtmlNS,''input'');
input.setAttribute(''width'',''20em'');

我本来可以调用input.width ='''20em''

什么时候更好用,或者它们之间没有区别?


首先,知道区别是什么很重要。


前者是一般的DOM方法。它适用于所有DOM文档中

设置元素的属性。


后者来自HTML DOM(以及样式和事件DOM),

是基本DOM的扩展。它只适用于HTML / XHTML

文件。


人们可以希望使用

node.attrName =" foo"

相当于

node.setAttribute(" attr-name"," foo")

(由

< URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-642250288>),

但是在所有浏览器中情况并非如此。

在IE浏览器中,也可能在其他浏览器中,DOM属性表示原始值为

,但不是直播 ;。要更改渲染器使用的实际值

,您必须分配给该属性。

此外,以这种方式使用setAttribute是不合适的:
input.setAttribute(''onchange'',mychangefunction)




是的。值必须是DOMString类型。

例如,HTML

onclick属性和DOM属性之间并没有真正的标准化连接。最接近的是

< URL:http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration-html40>

要遵循标准,您应该使用Events DOM方法

(addEventListener等)来处理事件处理程序。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡色彩:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断力的信仰只会降低精神神圣。''


I am curious if there is a benefit to set attributes directly, in my
javascript, or to use setAttribute.

For example, I have this:
var input = document.createElementNS(xhtmlNS, ''input'');
input.setAttribute(''width'', ''20em'');

I could have just called input.width=''20em''

When is each better to use, or is there no difference between them?

Also, it is improper to use setAttribute in such a way:
input.setAttribute(''onchange'', mychangefunction)

Thank you for any help.

解决方案

James Black wrote:

Also, it is improper to use setAttribute in such a way:
input.setAttribute(''onchange'', mychangefunction)


Have you tried it? I haven''t.

Even if it does work, it would be obscure, just assign event handlers
directly.

--
Ian Collins.


James Black said the following on 5/20/2006 9:40 PM:

I am curious if there is a benefit to set attributes directly, in my
javascript, or to use setAttribute.
Yes, there is a benefit to setting the attribute directly. setAttribute
is known to be buggy in IE, so why bother with a buggy code?
For example, I have this:
var input = document.createElementNS(xhtmlNS, ''input'');
input.setAttribute(''width'', ''20em'');

I could have just called input.width=''20em''
And that is a better approach.
When is each better to use, or is there no difference between them?
The difference is buggy behavior in IE with some setAttribute calls.
Also, it is improper to use setAttribute in such a way:
input.setAttribute(''onchange'', mychangefunction)



No idea, never used it before. input.onchange = mychangefunction;

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


"James Black" <pl***********@gmail.com> writes:

I am curious if there is a benefit to set attributes directly, in my
javascript, or to use setAttribute.

For example, I have this:
var input = document.createElementNS(xhtmlNS, ''input'');
input.setAttribute(''width'', ''20em'');

I could have just called input.width=''20em''

When is each better to use, or is there no difference between them?
First of all, it''s important to know what the difference is.

The former is a general DOM method. It works in all DOM documents to
set the attribute on the element.

The latter comes from the HTML DOM (and Style and Events DOMs), which
is an extension of the basic DOM. It only really applies to HTML/XHTML
documents.

One could then hope that using
node.attrName = "foo"
was equivalent to
node.setAttribute("attr-name", "foo")
(as suggested by
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-642250288>),
but that is not the case in all browsers.
In IE, and possibly other browsers too, the DOM attribute represents
the original value, but isn''t "live". To change the actual value
used by the renderer, you must assign to the property.
Also, it is improper to use setAttribute in such a way:
input.setAttribute(''onchange'', mychangefunction)



Yes. The value must be of type DOMString.
There isn''t really a standardized connection between, e.g., the HTML
onclick attribute and a DOM attribute. The closest is
<URL:http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration-html40>
To follow standards, you should use the Events DOM methods
(addEventListener, etc) to handle event handlers.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


这篇关于直接使用setAttribute或set属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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