为了在翻译上显示出区别,Attribute一般被翻译为特性,Property被译为属性。
在使用上面,Angular已经表明态度

Template binding works with properties and events, not attributes.
模板绑定是通过 property 和事件来工作的,而不是 attribute。

jQuery中的prop()attr()如何选择,众说纷纭...

两种主流观点:

  1. 对于一些公认的attribute和property,使用setAttribute(),理由是property会出现class映射过去为className,名称不统一的问题。
  2. 红宝书作者推荐操作DOM property,因为在浏览器上面表现的比较一致。

HTML attribute & DOM property 关系与区别

引用Angular文档中的一段话去概括两者的关系和区别:

HTML attribute 与 DOM property 的对比
attribute 是由 HTML 定义的。property 是由 DOM (Document Object Model) 定义的。
少量 HTML attribute 和 property 之间有着 1:1 的映射,如id。
有些 HTML attribute 没有对应的 property,如colspan。
有些 DOM property 没有对应的 attribute,如textContent。

普遍原则

  1. HTML attribute 初始化 DOM property,然后它们的任务就完成了。
  2. 更改 attribute 的值,相当于再次初始化DOM property 。
  3. 更改 property 的值,property值改变,attribute值不变。

几个有代表性的映射表

HTML attribute DOM property
id id
class className
checked = 'checked' checked 值为true

普遍原则失效的情况

为什么说是普遍原则呢?在低版本的ie中,操作DOM property中的value,attribute中的value也发生了改变。完全不讲道理 - -

<input type="text" value="123" id="myInput">

更改HTML attribute

myInput.setAttribute('value', 'test Attr');  
浏览器 myInput.getAttribute('value') myInput.value
chrome ie11 ff test Attr test Attr
ie8 test Attr test Attr

更改DOM property

document.getElementById('myInput').value = 'test property'; 
浏览器 myInput.getAttribute('value') myInput.value
chrome ie11 ff 123 test property
ie8 test property (普遍原则下应该为123) test property

prop()attr()的选择

  • prop()采用的是更改DOM property的方式,基本上对应更改DOM属性。
原生DOM jQuery 操作
element.value $element.prop( name[,value]) 读写
delete element.propertyName $element.removeProp( propertyName ) 删除
  • attr() 采用的是更改HTML attribute的方式,基本上对应DOM中提供的三个操作attribute的方法。
原生DOM jQuery 操作
element.getAttribute(name) $element.attr(name)
element.setAttribute(name,value) $element.attr(name ,value)
delete element.removeAttribute(name) $element.removeAttr( name ) 删除

小结

  • 获取一些标签上面的的自定义属性,或者一些基本不会改变的特性的时候,多数情况下用attr()data-*等除外)。
  <form action="test.php" user-my-name="nilinli" method="post"></form>
  $('form').attr('user-my-name'); // nilinli
$('form').attr('action'); // test.php
  • 其他情况下,操作DOM与页面交互,一般情况下用prop()
  • 总的来说,尽量操作DOM property,只有在没有DOM property(自定义attribute或者没有相关映射),才去操作HTML attribute。

前端中的 Attribute & Property的更多相关文章

  1. C#中的Attribute Property区别

    Attribute 一般译作"特性",Property 仍然译为"属性". Attribute 是一种可由用户自由定义的修饰符(Modifier),可以用来修饰 ...

  2. HTML中的attribute和property

    一.概述 attribute和property是常常被弄混的两个概念. 简单来说,property则是JS代码里访问的: document.getElementByTagName('my-elemen ...

  3. 浅析C#中的Attribute(转)

    最近用到了,所以静下心来找些资料看了一下,终于把这东西搞清楚了. 一.什么是Attribute 先看下面的三段代码: 1.自定义Attribute类:VersionAttribute [Attribu ...

  4. 关于C# 中的Attribute 特性

    关于C# 中的Attribute 特性 作者: 钢钢  来源: 博客园  发布时间: 2011-01-09 23:30  阅读: 13921 次  推荐: 12   原文链接 [收藏] 摘要:纠结地说 ...

  5. C#中的Attribute

    最近用到了,所以静下心来找些资料看了一下,终于把这东西搞清楚了. 一.什么是Attribute 先看下面的三段代码: 1.自定义Attribute类:VersionAttribute [Attribu ...

  6. Element link doesn't have required attribute property

    前端标准http://validator.w3.org/ 拒绝你的代码时报 Element link doesn't have required attribute property 把样式链接 &l ...

  7. 浅析C#中的Attribute

    原文地址:http://www.cnblogs.com/hyddd/archive/2009/07/20/1526777.html 一.什么是Attribute 先看下面的三段代码: 1.自定义Att ...

  8. C#中的Attribute详解(下)

    原文地址:https://blog.csdn.net/xiaouncle/article/details/70229119 C#中的Attribute详解(下) 一.Attribute本质 从上篇里我 ...

  9. 说说移动前端中 viewport (视口)

    转载网络资源的文章:来源不详~~ 移动前端中常说的 viewport (视口)就是浏览器显示页面内容的屏幕区域.其中涉及几个重要概念是 dip ( device-independent pixel 设 ...

随机推荐

  1. O048、掌握 cinder-scheduler 调度逻辑

    参考https://www.cnblogs.com/CloudMan6/p/5589707.html   上一节我们详细讨论了cinder-api 和 cinder-volume ,今天讨论另一个重要 ...

  2. require.context() 用于获取一个特定上下文的,webpack的一个api

    参考链接: 1.https://www.jianshu.com/p/c894ea00dfec 2.https://www.jianshu.com/p/c894ea00dfec require.cont ...

  3. Struts简单的实例

    一.创建Aciton类 package com.my.frame; public class HelloWordAction { private String name; public String ...

  4. 通俗讲解python__new__()方法

    目录 通俗讲解python__new__()方法 引子: 小结: 通俗讲解python__new__()方法 转载于别人的博客https://blog.csdn.net/sj2050/article/ ...

  5. IoT 设备通信安全讨论

    IoT 设备通信安全讨论 作者:360CERT 0x00 序言 IoT 设备日益增多的今天,以及智能家居这一话题愈发火热,智能家居市场正在飞速的壮大和发展,无数 IoT 设备正在从影片中不断的走向用户 ...

  6. linux获取保留yum源、并获取安装位置

    linux使用yum安装之后保留yum源 linux在使用yum安装之后默认会删除rpm包,那么如何保留呢? vim /etc/yum.conf 将这里的0改成1即可 linux安装的yum源在什么地 ...

  7. (转)I2C 上拉大小

    中断,GPIO,I2C等一般都是OC或者OD门,芯片内部无上拉电阻时,则外部必须加上拉电阻才能输出高电平.一般I/O端的驱动能力在2-4mA量级,OC或者OD门的导通电压为0.4V左右,手机中加在上拉 ...

  8. QTP(13)

    练习1:Flight4a 要求: a.录制Flight4a登录+购票+退出业务流程 b.实现登录1次,购票3次,退出1次 c.对Fly From.Fly to.航班实现随机参数化 随机参数化:Rand ...

  9. 【模板】多标记 LCT

    代码如下 #include <bits/stdc++.h> using namespace std; typedef long long LL; const int mod = 51061 ...

  10. 根据传入url请求,返回json字符串

    /** * 根据传入url请求,返回json字符串 * @param url * @return * @throws UnsupportedEncodingException */ public st ...