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 ...
随机推荐
- [Beta]第九次 Scrum Meeting
[Beta]第九次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/19 21:20 20min 大运村公寓6F寝室 附Github仓库:WEDO 例会照片 (一人回 ...
- SpringBoot框架 之 Thymeleaf
目录 Thymeleaf 添加启动器 创建模板文件夹 基本使用 综合使用 Thymeleaf 介绍 SpringBoot并不推荐使用jsp Thymeleaf 是一个跟 Velocity.FreeMa ...
- mysql事务回滚机制概述
应用场景: 银行取钱,从ATM机取钱,分为以下几个步骤 1 登陆ATM机,输入密码: 2 连接数据库,验证密码: 3 验证成功,获得用户信息,比如存款余额等: 4 用 ...
- 2019年10~11月-NLP工程师求职记录
求职目标:NLP工程师 为什么想换工作? 除了技术相关书籍,我没读过太多其他类型的书,其中有一本内容短但是对我影响特别大的书--<谁动了我的奶酪>.出门问问是我毕业后的第一份工作,无论是工 ...
- 支付宝即时到账交易接口C#接入方式的几个坑
1.在官方文档中 https://docs.open.alipay.com/62/104743 可以清楚看到input_charset前面没有要求加下横杠,可是请求示例是带着的.经过实验得知,这个必须 ...
- powshell 输出字符编码的问题,设置为utf-8
https://blog.csdn.net/qianxiao_1/article/details/79463409 $PSDefaultParameterValues['Out-File:Encodi ...
- mysql 中 int 等类型如何选择
详见:https://blog.csdn.net/samll_snail/article/details/86534719 .
- python2中的unicode()函数在python3中会报错:
python2中的unicode()函数在python3中会报错:NameError: name 'unicode' is not defined There is no such name in P ...
- WebGL学习笔记(三):绘制一个三角形
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java8 Collectors类的静态工厂方法
预定义收集器的功能,就是那些可以从Collectors类提供的工厂方法(例如grouping By)创建的收集器. 它们主要提供了三大功能: •将流元素归约和汇总为一个值 •元素分组 •元素分区 •c ...