Attribute与Property关系
总的来说,其实是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关系的更多相关文章
- jQuery的attr与prop,attribute和property区别
jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与property区别说起,attr与prop正是这两个东 ...
- Attribute和Property
有时很容易对Attribute和Property混淆,因为中文翻译都是“属性”来解释的.其实这两个表达的不是一个层面的东西. Property属于面向对象理论范畴,在使用面向对象思想编程的时候,常常需 ...
- boolean attribute(布尔值属性) attribute vs property
boolean attribute(布尔值属性) boolean attribute HTML - Why boolean attributes do not have boolean val ...
- C#中Attribute和Property
XAML是XML派生而来的语言,所以很多XML中的概念在XAML中是通用的. 为了表示同类标签中的某个标签与众不同,可以给它的特征(Attribute)赋值,为特征值赋值的语法如下: 非空标签:< ...
- HTML中的attribute和property
一.概述 attribute和property是常常被弄混的两个概念. 简单来说,property则是JS代码里访问的: document.getElementByTagName('my-elemen ...
- attribute和property的区别
DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...
- javascript中attribute和property的区别详解
DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...
- 作为 attribute 和 property 的 value 及 Vue.js 的相关处理
attribute 和 property 是 Web 开发中,比较容易混淆的概念,而对于 value,因其特殊性,更易困惑,本文尝试做一下梳理和例证 attribute 和 property 的概念 ...
- javascript DOM 操作 attribute 和 property 的区别
javascript DOM 操作 attribute 和 property 的区别 在做 URLRedirector 扩展时,注意到在使用 jquery 操作 checkbox 是否勾选时,用 at ...
随机推荐
- intellij ide 激活(转发)
前期准备 文件下载:jetbrains-agent.jar 激活码:license.txt 3AGXEJXFK9-eyJsaWNlbnNlSWQiOiIzQUdYRUpYRks5IiwibGljZW5 ...
- redis 使用redis Desktop manger进行远程进行链接
1.修改redis.conf文件: a.去掉bind:127.0.0.0 b.protected mode 模式改成 no 2.重启redis /etc/init.d/redis restart 3. ...
- 爬虫urllib2中Handler处理器和自定义Opener
Handler处理器 和 自定义Opener opener是 urllib2.OpenerDirector 的实例,urlopen是一个特殊的opener(也就是模块已经构建好的). 但是基本的url ...
- echo * 和ls *之间的区别?
背景描述: 今天 一同事做入职考试,涉及到1题目,echo * 和ls *之间的区别,没有用过这个用法,再次记录下. 操作过程: 1.执行echo * [root@localhost ~]# echo ...
- Base64(3)
public final class Base64 { static private final int BASELENGTH = 128; static private ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- shell request failed on channel 0
今天普通用户ssh 登录提示shell request failed on channel 0 然后就退出了 幸亏root 用户没有被禁用,在root下 su - 普通 切换提示资源不足 解决方法 ...
- Consul1.5.0 带ACL控制集群搭建
Consul Cluster with ACL1.机器规划2.先配置好三个Server,并启动一遍.3.生成并配置agent-token,解决server agent ACL block问题4.启动一 ...
- 【神经网络与深度学习】【计算机视觉】SSD
SSD 转自:https://zhuanlan.zhihu.com/p/24954433?refer=xiaoleimlnote 参考: SSD: Single Shot MultiBox Detec ...
- python time模块(13)
python time模块主要包含各种提供日期.时间功能的相关函数.time模块既提供了把日期.时间格式化为字符串的功能,也提供了从字符串恢复日期.时间的功能. 一.前言 在 time 模块内提供了很 ...