jQuery 创建元素 - 另一个 .attr() 与 .prop() 问题
可能的重复:
.prop() 与 .attr()
鉴于新的 .prop()
jQuery 方法,这是创建的首选方法具有特定字段的新元素,例如链接:
我一直倾向于使用#2,但不清楚放入 DOM 中的新元素现在是否不应该使用#1。
Possible Duplicate:
.prop() vs .attr()
Given the new .prop()
method in jQuery, which is the preferred way to create a new element with particular fields, e.g. a link:
$('<a>').prop('href', '...');
$('<a>').attr('href', '...');
$('<a href="...">');
I've always tended to use #2, but it's unclear whether a new element being put in the DOM shouldn't now use #1 instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下我会选择attr()。您正在创建一个新的
元素并设置其
href
HTML 属性,因此强调这一点是有意义的。但是,由于
href
属性直接映射到href
DOM 属性,因此使用prop()
将获得完全相同的结果。让我们举另一个例子,与
attr()
和prop()
之间的差异更相关:假设您要设置元素而不是其
href
。您问题中的示例代码变为:$('').prop('className', '...');
$('') .attr('class', '...');
$('');
恕我直言,(2) 和 ( 3)使自己的意图比(1)更清楚。事实上,不能有名为
class
的 DOM 属性,因为该标记可能是宿主语言中的保留字,这充其量只是一个实现细节。class
HTML 属性通常是我们在该上下文中考虑的内容。当然,在某些情况下,情况恰恰相反,例如使用“布尔”HTML 属性(例如
checked
或disabled
)时。在这种情况下,设置强类型 DOM 属性而不是创建定义不太明确的 HTML 属性会更加稳健。I would go with
attr()
in that case. You're creating a new<a>
element and setting itshref
HTML attribute, so it would make sense to emphasize that.However, since the
href
attribute directly maps to thehref
DOM property, usingprop()
would have the exact same result.Let's take another example, more relevant to the differences between
attr()
andprop()
: suppose you want to set theclass
attribute of the<a>
element instead of itshref
. The sample code in your question becomes:$('<a>').prop('className', '...');
$('<a>').attr('class', '...');
$('<a class="...">');
IMHO, (2) and (3) make one's intent clearer than (1). The fact that there cannot be a DOM property named
class
, because that token might be a reserved word in the host language, is at best an implementation detail. Theclass
HTML attribute is usually what we're thinking about in that context.Of course, there are situations where the opposite is true, e.g. when working with "boolean" HTML attributes like
checked
ordisabled
. In that case, it would be more robust to set the strongly-typed DOM property instead of creating the less well-defined HTML attribute.我通常会像这样创建 a 标签:
在开头向元素添加尽可能多的属性。 (是的
属性
,而不是属性)因此您可以对输入之类的项目执行相同的操作:
或实际的div:
这是一个小提琴示例:http://jsfiddle.net/maniator/mbjgd/
编辑
从我下面的评论来看:
这样做的原因是因为当您创建项目时,您是通过设置它的属性来创建它的,稍后当这些属性被诸如 javascript 之类的东西更改时就变得毫无意义,所以此时您必须使用对象的属性以获得您可能正在寻找的真正解决方案。
I usually would create the a tag like so:
Adds as many attributes to the element at the start. (yes
attributes
, not properties)So you can do the same thing for items like inputs:
Or actual divs:
Here is a fiddle example: http://jsfiddle.net/maniator/mbjgd/
EDIT
From my comment below:
The reason for this is because when you create the item, you are creating it by setting it's attributes, later on those attributes become meaningless when they are changed by something like javascript, so at that point you have to use the object's properties to get the real solution that you might be looking for.