GWT 元素 setAttribute 与 setPropertyString
我无法确定 Element.setAttribute(String name, String value) 和 Element.setPropertyString(String name, String value) 之间的区别。有区别吗?当尝试在文本输入上设置占位符时,哪个是首选?我一直在做 getElement().setPropertyString("placeholder", "this is a placeholder");
并且它有效,但是这是正确的方法吗?
在 DOM 文档中,setAttribute(Element, String, String)
已弃用,表示使用“更合适的命名 setElementProperty(Element, String, String)
代替”。这是否意味着应该使用 Element
类似命名的方法?
I can't determine the difference between Element.setAttribute(String name, String value)
and Element.setPropertyString(String name, String value)
. Is there a difference? Which is preferred when attempting to set, say, a placeholder on a text input? I've been doing getElement().setPropertyString("placeholder", "this is a placeholder");
and it works, but is that the appropriate way to do it?
In the documentation for DOM, setAttribute(Element, String, String)
is deprecated, saying to use "the more appropriately named setElementProperty(Element, String, String)
instead." Does that imply that one should be using the similarly named methods of Element
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
属性和特性之间是有区别的。简而言之,属性代表初始状态,而属性始终代表当前状态。
请参阅http://jquery-howto.blogspot。 com/2011/06/html-difference- Between-attribute-and.html 获取详细说明。
在 GWT 中,调用 setAttribute 会在当前元素上调用本机 javascript 函数 setAttribute。调用 setProperty... 设置当前元素的属性。
过去,在大多数浏览器中,这都是相同的,但随着标准的不断发展,这种情况开始发生变化。
我真的不知道浏览器实现之间的所有细微差异,但要跟踪差异,可以依靠不同的 DOM 级别规范:http://www.w3.org/TR/DOM-Level-2-HTML/ http://www.w3.org/TR/DOM-Level-3-Core/
此外,setAttribute 上的 Mozilla 文档也可以很好地说明 firefox 的差异: https://developer.mozilla.org/en/DOM/element.setAttribute
综上所述:如果你使用setAttribute GWT 您依赖于浏览器 setAttribute 实现,该实现在某种程度上设置默认值(在某些属性上,而不是更新值),因此通常您需要 setProperty...
There is a difference between Attributes and Properties. In short the attribute represents the initial state while the property always represents the current state.
See http://jquery-howto.blogspot.com/2011/06/html-difference-between-attribute-and.html for a detail explanation.
In GWT calling setAttribute calls the native javascript function setAttribute on the current element. Calling setProperty... set the property on the current element.
In the past this in most browsers this used to be the same, but with evolving standards this started to change ago.
I don`t really know all the small differences between browser implementations, but to track the difference one could rely on the different DOM Level Specs: http://www.w3.org/TR/DOM-Level-2-HTML/ http://www.w3.org/TR/DOM-Level-3-Core/
Also the Mozilla docs are on setAttribute are quite could and state the difference for firefox: https://developer.mozilla.org/en/DOM/element.setAttribute
So in summary: if you use setAttribute in GWT you rely on the browser setAttribute implementation which is somewhat setting the defualt value (on certain properties, and not updating a value), so normally you want setProperty...
问题是,在 IE6 和 IE7(以及兼容模式下的 IE8)中,
setAttribute
实际上设置了属性(IE 并没有真正产生区别;IE8 向getAttribute
添加了一个可选参数code> 允许检索 DOM 规范中定义的属性;请参阅 http://msdn.microsoft.com/en-us /library/ms536429v=vs.85.aspx)。顺便说一句,您的 JavaDoc 参考应该是 http://google-web- toolkit.googlecode.com/svn/javadoc/latest/index.html(不是 GWT 1.5 的版本,它已经严重过时了),您应该使用
com.google.gwt.dom.client.Element
而不是com.google.gwt.user.client.DOM
。Element
有一个setAttribute
,它可以在 IE6/7(或 IE8 的类似模式)之外的每个浏览器中设置属性。但大多数时候,您应该只使用 DOM 属性,而不是属性。例如,您希望将标签索引获取为数字,而不是字符串。并且您希望在没有属性的情况下获得属性的默认值/状态,而不是您必须自己处理的
null
(例如input
元素默认为type=text
当没有type
属性时;getAttribute("type")
将返回null
而getPropertyString("type")
将返回“文本”
)。The problem is that in IE6 and IE7 (and IE8 in compatibility modes),
setAttribute
actually sets the property (IE doesn't really make a difference; IE8 added an optional argument togetAttribute
to allow retrieving the attribute as defined in the DOM spec; see http://msdn.microsoft.com/en-us/library/ms536429v=vs.85.aspx).BTW, your JavaDoc reference should be http://google-web-toolkit.googlecode.com/svn/javadoc/latest/index.html (not the one for GWT 1.5, which is severely outdated), and you should use
com.google.gwt.dom.client.Element
rather thancom.google.gwt.user.client.DOM
.Element
has asetAttribute
that sets the attribute in every browser other than IE6/7 (or similar modes of IE8).But most of time, you should just work with DOM properties, rather than attributes. For instance, you want to get the
tab index
as a number, not as a string. And you want the default value/state for a property in the absence of the attribute, not anull
that you'd have to handle yourself (e.g. aninput
element defaults totype=text
when there's notype
attribute;getAttribute("type")
would returnnull
whereasgetPropertyString("type")
would return"text"
).