In this short post I will explain the difference between attributes and properties in HTML. The .prop() function introduced in jQuery 1.6 raised a lot of questions about the difference and I hope this post will help you to understand it.

What is an attribute?

Attributes carry additional information about an HTML element and come in name=”value” pairs.
Example:
<div class=”my-class”></div>. Here we have a div tag and it has a class attribute with a value of my-class.

What is a property?

Property is a representation of an attribute in the HTML DOM tree. So the attribute in the example above would have a property named className with a value of my-class.

Our DIV node
|- nodeName = "DIV"
|- className = "my-class"
|- style
 |- ...
|- ...

What is the difference between attribute and property?

Attributes are in your HTML text document/file, whereas properties are in HTML DOM tree. This means that attributes do not change and always carry initial (default) values. However, HTML properties can change, for example when user checks a checkbox, inputs text to textarea or uses JavaScript to change the property value.

Here is a visual representation:

Assume user inputs his name "Joe" into the inputbox. Here are what attribute and property values of an element will be.

As you can see, only element’s property is changed, because it is in the DOM and dynamic. But element’s attribute is in HTML text and can not be changed!

I hope this helps to understand the difference between attributes and properties. If you have any questions please leave them on jQueryHowto Facebook page.

Also, stay tuned for the next post about the difference between jQuery.attr() and jQuery.prop() and when to use one over another.

简而言之:attribute是相对于html 代码而言,你可以在浏览器对象检查器中看到;
而property,是对Dom而言,还会包含对样式方面的信息,更多是用在数据编辑时使用

更多相关文章

  1. jquery 添加transform样式
  2. 只对css类进行动画处理,没有显式样式
  3. ie8下jQuery动画没有ie7流畅,是浏览器性能问题吗?有没有什么解决方
  4. CSS样式如何解决IE浏览器不同版本的兼容问题
  5. 关于INPUT的autocomplete="off" 属性(浏览器表单默认的记忆功能)
  6. html 加上readonly后在各种浏览器的差异。
  7. html中显示div的时候,超出浏览器的宽,怎么解决,在线等!!!
  8. 我无法在某些Web浏览器中输入我的注册文本字段
  9. Jquery - 表单验证,为错误消息添加css样式

随机推荐

  1. Android 不依赖activity的全局悬浮窗实现
  2. android:screenOrientation 强制屏幕方向
  3. Android EditText 代码实现键盘弹出打开
  4. android 整合 maven 环境
  5. 有关谷歌Android和Java历史的一课
  6. Android JUnit单元测试
  7. Android数据库操作的两种方式
  8. android 中限制EditText 最大输入字符数
  9. Google Maps API Key申请方法及地址
  10. Android解析自定义xml文件--Sax解析xml文