有关attribute和property,以及各自对select中option的影响

这个问题老生常谈,但是直到现在我依旧时常会把它搞混。下面列一些各自的特性。

  attribute property
设置方法
option.setAttribute(‘selected‘, true)

option.getAttribute(‘selected‘)

option.selected = true
dom节点表现
会表现在html节点上。打开控制台,可以看到

<option selected=true></option>


不会表现在html中。打开控制台,孤零零的

:<option></option>

数据类型
当你把一个对象传给attribute时,它会转为string类型,

根据dom节点表现特点,很好理解,html代码就是字符串不可能有什么数据类型。

比如:

option.setAttribute(‘test‘, {})

option.getAttribute(‘test‘)

你将得到"[object object]",它自动调用了Object.toString方法


而当你把一个对象传给property时,你可以保留这个对象。

比如:

option.test = {};

这就是代码层级的东西,跟html一点关系都没有

由上面我们可以看到:

attribute总是试图与html代码串产生关联,对attribute的操作会直接反映在页面html代码中。attribute的这个特性可以让我们在js代码执行前就定义好某些变量,然后在js执行时获取它们,虽然这些变量都是字符串类型,但是提前定义变量可以让我们实现很多事,它被广泛的应用在smarty模板中。

如果要在js执行过程中保存变量到某dom节点,不妨使用property,它可以把变量类型完美的保存下来,因此可以把dom节点上内置实现的函数浅显的理解为property。

下面以select,option为栗子,谈一下浏览器执行时,这两个东西的表现。

<select>

<option id="one" value="1">北京</option>

<option id="two"value="2">上海</option>

</select>

代码 property attribute 选中状况

one: true

two: falss


one: null

two: null


默认状态,one选中


设置two的attribuite,让它选中

two.setAttrbute(‘selected‘, true)

one: false

two: true

one: null

two: true


two选中

虽然我们设置的是attribuite,但是property发生了变化。

这里one.selected自动变为false。two.selected自动变为true


然后我们设置one的attrbute,让它选中

one.setAttribute(‘selected‘, true)

one: true

two: false


one: true

two: true


one选中

设置one的attribute后,property同上一次一样,自动做出了相应的变化,

但是attribute严格按照我们的代码,保留了我们设置的属性,所以出现了两个attribute都是true的情况。


然后我们再次设置two的attribuite,让它选中

two.setAttrbute(‘selected‘, true)

one: false

two: true

one: true

two: true

two选中

我们可以看到,attibuite没有变化

property,更像是浏览器的当前状态的一个反应,不只代码影响它,用户的交互也可以影响它。而attibute仅仅依赖代码的执行,仅仅你的代码可以改变它。

而对于浏览器控件来说,会有一些特殊的属性来决定它的表现与动作,如value,selected等。实质上起作用的是property的值。但是当我们设置attribute时,也会自动触发propery的改变(从上表格可以看出,不管attribute值变化与否,只要赋值就会引发propery的变化),从而达到控制控件动作样式的目的。相对的,我们设置propery时,不会改变attribute,只能控制控件的动作样式。

时间: 2024-08-11 09:48:10

有关attribute和property,以及各自对select中option的影响的相关文章

js 获取select 中option 的个数

//document.writeln(document.getElementById("sel").options.length); //document.writeln(document.getElementById("sel")["options"].length); //document.writeln(document.getElementById("sel").children.length);

attribute和property兼容性分析

上一篇文章中,详细的分析了他们的区别,请看Javascript中的attribute和property分析 这次,来详细的看下他们的兼容性,这些内容主要来自于对于jQuery(1.9.x)源代码的分析(attributes模块),首先,贴出测试的HTML代码: <input id="username" type="text" value="lonelyclick"> <button value="abc" i

javascript中attribute和property的区别详解

DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为"特性",property翻译成中文术语为"属性",从中文的字面意思来看,确实是有点区别了,先来说说attribute. attribute是一个特性节点,每个DOM元素都有一个对应的attributes属性来存放所有的attribute节点,attributes是一个类数

attribute与property区别总结

在前阵子看JQuery源码中,attr()的简单理解是调用了element.getAttribute()和element.setAttribute()方法,removeAttr()简单而言是调用element.removeAttribute(),而prop()简单理解是element.xxx方式存取属性,removeProp()是通过delete element.xxx方式删除. attribute与property都是属性的意思.那么有何区别呢? 对于这个问题,今天问了好几个群,也找到一些文章

attribute和property

前言:attribute和property分别翻译为"特性"和"属性",这两者很容易混淆,本文主要介绍它们的异同. attribute特性 [定义] dom元素在文档中作为html标签拥有一些特性,比如id,class,title等标准特性,或开发人员自定义的特性. <div id="myDiv" class="bd" title="bodyText" myProp="hello"

attribute和property的区别

DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特性”,property翻译成中文术语为“属性”,从中文的字面意思来看,确实是有点区别了,先来说说attribute. attribute是一个特性节点,每个DOM元素都有一个对应的attributes属性来存放所有的attribute节点,attributes是一个类数组的容器,说得准确点就是Nam

HTML中的attribute和property

一.概述 attribute和property是常常被弄混的两个概念. 简单来说,property则是JS代码里访问的: document.getElementByTagName('my-element').prop1 = 'hello'; attribute类似这种: <my-element attr1="cool" /> JS代码里访问attribute的方式是getAttribute和setAttribute: document.getElementByTagName

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

boolean attribute(布尔值属性) boolean attribute     HTML - Why boolean attributes do not have boolean value?     Boolean HTML Attributes   HTML Boolean Attributes A number of attributes are boolean attributes. The presence of a boolean attribute on an ele

Javascript中的attribute和property分析

attribute和property这两个单词,都有属性的意思,attribute有属性.特质的意思,property则有性质,性能的意思. 首先需要明确的是,在规范中,读取和设置attribute的方法是getAttribute/setAttribute/removeAttribute,比如box.setAttribute('somekey','somevalue') 而想要访问元素的property,则需要用.(点)的方法,比如,box.somekey,下面先说attribute: <div