总的来说,其实是HTML Attribute 与 DOM property之间的关系。

1 什么是Property?

JS DOM Object对象有property。一个property可能是不同数据类型的(boolean,string,etc.),而且这些property普遍都是标准的(即可以通过'.'操作符直接引用property的值)。

 <a href='page2.html' class='link classes' name='linkName' id='linkID'>Hi</a> // obj.href obj.className obj.name obj.id etc.

2 什么是Attribute?

Attribute只是针对HTML本身(即HTML标签内直接写着的),一个attribute只有String一种数据类型。

 <input type="checkbox" checked=true/>
$('input').prop('checked'); // property -> returns true
$('input').attr('checked'); // attribute -> returns "checked"

3 不同点

a 如果一个元素有一个默认value,attribute总是显示Html本身上的值,即使property已经对value做出了改变。

 <input type="text" name="username" value="user123">
$('input').prop('value', '456user'); //改变property值
$('input').prop('value'); // returns "456user"
$('input').attr('value'); // returns "user123"

b HTML元素标签内定义属性对Attribute和Property影响。

标准属性概念是对于DOM Object存在的,比如id,className,dir,etc.
定义标准属性,同时影响attribute和property
定义非标准属性,只在attribute中存在,而不在property
1 <input type="text" custom="something">
$('input').attr('custom'); // returns "something"
$('input').prop('custom'); // returns undefined

c 代码更改属性对Attribute和Property影响。

 // 添加标准属性,同时影响attribute和property
1 <div id="test" class="button"></div>
var div = document.getElementById('test');
div.className = 'red-input';
div.getAttribute('class'); // return string: "red-input"
div.setAttribute('class','green-input');
div.className; // return string: "green-input"
// 添加非标准属性,互相不影响
7 div.jjj = 123;
div.getAttribute('jjj'); // return null
div.setAttribute('lll', 123);
div.lll; // return undefined;

Attribute与Property关系的更多相关文章

  1. jQuery的attr与prop,attribute和property区别

    jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与property区别说起,attr与prop正是这两个东 ...

  2. Attribute和Property

    有时很容易对Attribute和Property混淆,因为中文翻译都是“属性”来解释的.其实这两个表达的不是一个层面的东西. Property属于面向对象理论范畴,在使用面向对象思想编程的时候,常常需 ...

  3. boolean attribute(布尔值属性) attribute vs property

    boolean attribute(布尔值属性) boolean attribute     HTML - Why boolean attributes do not have boolean val ...

  4. C#中Attribute和Property

    XAML是XML派生而来的语言,所以很多XML中的概念在XAML中是通用的. 为了表示同类标签中的某个标签与众不同,可以给它的特征(Attribute)赋值,为特征值赋值的语法如下: 非空标签:< ...

  5. HTML中的attribute和property

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

  6. attribute和property的区别

    DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...

  7. javascript中attribute和property的区别详解

    DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...

  8. 作为 attribute 和 property 的 value 及 Vue.js 的相关处理

    attribute 和 property 是 Web 开发中,比较容易混淆的概念,而对于 value,因其特殊性,更易困惑,本文尝试做一下梳理和例证 attribute 和 property 的概念 ...

  9. javascript DOM 操作 attribute 和 property 的区别

    javascript DOM 操作 attribute 和 property 的区别 在做 URLRedirector 扩展时,注意到在使用 jquery 操作 checkbox 是否勾选时,用 at ...

随机推荐

  1. 再见,Eclipse。

    阅读本文大概需要 5 分钟. 来源:cnblogs.com/ouyida3/p/9901312.html 最近,改用了 IDEA,同事都说我投敌了.当然,这些同事都是和我一样的“老”程序员.不说毕业生 ...

  2. ACR Code Pacs

    ACR Index for Radiological Diagnosis 简称ACR Index,ACR Key或ACR Code,是一种应用于影像学分类的病理编码,由美国放射学院(American ...

  3. Android NFC M1卡读写&芯片卡读写(CPU卡读写)(RFID读写)

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/sgn5200/article/detai ...

  4. (8)Flask微电影项目会员中心其他页面搭建

    会员中心修改密码.评论.登录日志和收藏电影4个页面的内容. 一.修改密码页面: {% extends "home/home.html" %} {% block css %} < ...

  5. 【转】Python访问oracle数据库,DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found"

    使用python连接Oracle,出现如下错误: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified ...

  6. Bazel安装及使用入门

    Bazel [文档][https://docs.bazel.build/versions/1.1.0/bazel-overview.html] MacOS安装 brew tap bazelbuild/ ...

  7. Azure上部署Barracuda WAF集群 --- 2

    前面一篇文章讲了如何在Azure上部署Barracuda.这篇文章聊一聊如何配置Barracuda. License 向Barracuda的销售人员申请WAF的License.得到License后打开 ...

  8. 【视频开发】【计算机视觉】doppia编译之三:编译安装opencv库

    这里我介绍2种方法 (1)利用别人写好的脚本编译,相对来说省力一点  上Github下载别人写好的脚本文件,网址 https://github.com/jayrambhia/Install-OpenC ...

  9. objectARX2010及其以上版本使用publish打印(发布)图纸,后台布局打印图纸例子浅析

    AutoCAD 2010版本开始新增了一个发布图纸的功能,可以后台打印图纸,以下是ADN官方博客例子浅析 原文地址 https://adndevblog.typepad.com/autocad/201 ...

  10. Redis 主从配置密码以及哨兵

    目录: Redis 主从介绍 哨兵机制 Redis 主从配置 环境 安装 启动服务 检查主从状态 测试数据同步 默认是读写分离的 Redis Sentinel 配置 主Redis宕机测试 配置多个哨兵 ...