jQuery attr vs. prop,有一个道具列表? [英] jQuery attr vs. prop, there are a list of props?

查看:140
本文介绍了jQuery attr vs. prop,有一个道具列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是重复的问题,我只需要判断更好/更快/更正是否使用 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屋!

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