jQuery attr vs. prop,有一个道具列表? [英] jQuery attr vs. prop, there are a list of props?
问题描述
这不是重复的问题,我只需要判断更好/更快/更正是否使用 attr
或使用 prop
。最简单可靠的方法是检查列表。一个元素名称列表,其中更好的是使用 prop(名称)
和/或更好用的列表 attr(名称)
。
This is not a duplicate question, I need only to decide if the better/fast/correct is to use attr
or to use prop
. The simplest and reliable way is checking into a list. A "list of element-name where the better is use prop(name)
and/or a list where the better is use attr(name)
".
PS:我使用的是jQuery 1.9+,并假设 attr( )
不是弃用的方法。
PS: I am using jQuery 1.9+, and suppose that attr()
is not a deprecated method.
决策示例(其中 LIST 得到答案):
- 最好通过
$(x)。prop('id')获取ID值
或$(x).attr('id')
? - 更好的更改标题
$(x).prop('title','BLABLA')
或$(x).attr('title','BLABLA' )
? - 我可以使用
$(x).attr('selectedIndex')
? - 跨浏览器一致性有哪些属性名称存在风险?
- 有一些情况(使用Microsoft-IE p.ex.)其中
$(x).attr('name')
和$(x).prop('name')
将返回不同的东西?
- is better get ID value by
$(x).prop('id')
or by$(x).attr('id')
? - is better change title with
$(x).prop('title','BLABLA')
or with$(x).attr('title','BLABLA')
? - can I use
$(x).attr('selectedIndex')
? - what property names have risk with "Cross-browser consistency"?
- there are some case (with Microsoft-IE p.ex.) where
$(x).attr('name')
and$(x).prop('name')
will return different things?
也许不需要完整列表,只列出一些对jQuery产生一些影响或风险的事情。
Perhaps a full list is not necessary, only a list of things that make some difference or risk for jQuery.
关闭后编辑:请点击下面的 REOPEN 链接接受此编辑的问题文本。
Edited after close: please click in the REOPEN link below to accept this edited question text.
推荐答案
查看 https://开发人员.mozilla.org / zh-CN / docs / DOM / element#Properties 并且还考虑到许多属性仅适用于特殊类型的元素。
Have a look at https://developer.mozilla.org/en-US/docs/DOM/element#Properties and also consider that many properties are only available on special types of elements.
这篇关于jQuery attr vs. prop,有一个道具列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!