GWT 元素 setAttribute 与 setPropertyString

发布于 2024-12-22 13:21:02 字数 571 浏览 2 评论 0原文

我无法确定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桜花祭 2024-12-29 13:21:02

属性和特性之间是有区别的。简而言之,属性代表初始状态,而属性始终代表当前状态。

请参阅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...

裂开嘴轻声笑有多痛 2024-12-29 13:21:02

问题是,在 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.DOMElement 有一个 setAttribute,它可以在 IE6/7(或 IE8 的类似模式)之外的每个浏览器中设置属性。

但大多数时候,您应该只使用 DOM 属性,而不是属性。例如,您希望将标签索引获取为数字,而不是字符串。并且您希望在没有属性的情况下获得属性的默认值/状态,而不是您必须自己处理的 null (例如 input 元素默认为type=text 当没有 type 属性时;getAttribute("type") 将返回 nullgetPropertyString("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 to getAttribute 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 than com.google.gwt.user.client.DOM. Element has a setAttribute 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 a null that you'd have to handle yourself (e.g. an input element defaults to type=text when there's no type attribute; getAttribute("type") would return null whereas getPropertyString("type") would return "text").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文