jQuery笔记三——text/html/val/attr/prop
1.获得内容
三个简单实用的用于 DOM 操作的 jQuery 方法:
- text() - 设置或返回所选元素的文本内容
- html() - 设置或返回所选元素的内容(包括 HTML 标记)
- val() - 设置或返回表单字段的值
<p id="test">This is some <b>bold</b> text in a paragraph.</p>
<p>Name: <input type="text" id="test2" value="Mickey Mouse"></p>
$("#test").text() ------ This is some bold text in a paragraph.
$("#test").html() ----- This is some <b>bold</b> text in a paragraph.
$("#test2").val() ------ Mickey Mouse
2.获得属性
jQuery attr() 方法用于获取属性值
<p><a href="http://www.w3cschool.cc" id="w3s">W3Cschool.cc</a></p>
$("#w3s").attr("href") ------ http://www.w3cschool.cc
3.设置内容和属性
还是上面的text、html、val、attr.
attr同时设置多个属性:
$("#w3s").attr({
"href" : "http://www.w3cschool.cc/jquery",
"title" : "W3Schools jQuery Tutorial"
});
4.attr和prop的区别
相比attr,prop是1.6.1才新出来的,两者从中文意思理解,都是获取/设置属性的方法(attributes和properties)。只是,window或document中使用.attr()方法在jQuery1.6之前不能正常运行,因为window和document中不能有attributes。prop应运而生了。
参考:http://www.javascript100.com/?p=877
在遇到要获取或设置checked,selected,readonly和disabled等属性时,用prop方法显然更好
attr方法里面,最关键的两行代码,elem.setAttribute( name, value + “” )和ret = elem.getAttribute( name ),很明显的看出来,使用的DOM的API setAttribute和getAttribute方法操作的属性元素节点。
而prop方法里面,最关键的两行代码,return ( elem[ name ] = value )和return elem[ name ],你可以理解成这样document.getElementById(el)[name] = value,这是转化成JS对象的一个属性。
补充:在很多英文文档里面,提到过attribute只能返回string的结果,因此对于上面的这种bool值,或者其他的,类似style(??),size(数字)等的值,最好是用prop取。很多人的观点是大部分情况用prop,只有prop取不到值的时候用attr,除了两者取值不同(href)的地方要留意。而且大部分观点认为prop比attr要快。
几个典型的区别关键词:checked,style,value。checked在radio和checkBox里面用到,attr会取值"checked",而prop会取值true/false。style如果在html里面没有定义,attr会取值undefined,而prop无论什么情况都会取到一个object,里面包含各个style的属性值。用在input里面的value,attr会取到<input value="xxx">里面的"xxx",而prop的value则相当于.val()。很多情况和attr的value不是一个值。
attr —— Html attributes
prop —— Dom properties
- You usually want
prop()rather thanattr().- In the majority of cases,
prop()does whatattr()used to do. Replacing calls toattr()withprop()in your code will generally work.- Properties are generally simpler to deal with than attributes. An attribute value may only be a string whereas a property can be of any type. For example, the
checkedproperty is a Boolean, thestyleproperty is an object with individual properties for each style, thesizeproperty is a number.- Where both a property and an attribute with the same name exists, usually updating one will update the other, but this is not the case for certain attributes of inputs, such as
valueandchecked: for these attributes, the property always represents the current state while the attribute (except in old versions of IE) corresponds to the default value/checkedness of the input (reflected in thedefaultValue/defaultCheckedproperty).- This change removes some of the layer of magic jQuery stuck in front of attributes and properties, meaning jQuery developers will have to learn a bit about the difference between properties and attributes. This is a good thing.
However this point now begs the question: Why does .prop() exist?
In short, for two reasons:
- There are legitimate use cases for interacting with some DOM properties (such as
nodeName,selectedIndex, ordefaultValue) and we want to provide a simple solution for accessing, and mutating, them.- Accessing properties through the
.attr()method will be slightly slower than accessing them directly through.prop()(as.attr()calls.prop()internally in order to handle all property-related mutation).
<a href='foo.html' class='test one' name='fooAnchor' id='fooAnchor'>Hi</a>+-------------------------------------------+
| a |
+-------------------------------------------+
| href: "http://example.com/foo.html" |
| name: "fooAnchor" |
| id: "fooAnchor" |
| className: "test one" |
| attributes: |
| href: "foo.html" |
| name: "fooAnchor" |
| id: "fooAnchor" |
| class: "test one" |
+-------------------------------------------+
the vast majority of the time, we want to be working with properties. Partially that's because their values (even their names) tend to be more consistent
across browsers. We mostly only want to work with attributes when there is no property related to it (custom attributes), or when we know that for
that particular attribute, the attribute and the property are not 1:1 (as withhrefand "href" above)
Usually you'll want to use properties. Use attributes only for:
- Getting a custom HTML attribute (since it's not synced with a DOM property).
- Getting a HTML attribute that doesn't sync with a DOM property, e.g. get the "original value" of a standard HTML attribute, like
<input value="abc">.
官方对attr的介绍:http://api.jquery.com/attr/,对prop的介绍:http://api.jquery.com/prop/
最后附一张图:

jQuery笔记三——text/html/val/attr/prop的更多相关文章
- 『jQuery』.html(),.text()和.val()的使用
『jQuery』.html(),.text()和.val()的使用 2013-04-21 10:25 by 我是文东, 8335 阅读, 0 评论, 收藏, 编辑 本节内容主要介绍的是如何使用jQue ...
- jQuery中的text(),html(),val()用法
jQuery中的text(),html(),val()用法 text():获取或者改变指定元素的文本 html():获取或改变指定元素的html元素以及文本 val():获取或者改变指定元素的valu ...
- jQuery学习笔记—— .html(),.text()和.val()的使用
本节内容主要介绍的是如何使用jQuery中的.html(),.text()和.val()三种方法,用于读取,修改元素的html结构,元素的文本内容,以及表单元素的value值的方法.jQuery中为我 ...
- jquery的.html(),.text()和.val()方法
新人一段时间没写前端代码就有点忘记了,现在来复习一下..html()方法 获取集合中第一个匹配元素的HTML内容 或 设置每一个匹配元素的html内容,具体有3种用法: .html() 不传入值,就是 ...
- jQuery的.html(),.text()和.val()的概述及使用
本节内容主要介绍的是如何使用jQuery中的.html(),.text()和.val()三种方法,用于读取,修改元素的html结构,元素的文本内容,以及表单元素的value值的方法.jQuery中为我 ...
- 『jQuery』.html(),.text()和.val()的概述及使用
转自http://www.jb51.net/article/35867.htm 如何使用jQuery中的.html(),.text()和.val()三种方法,用于读取,修改元素的html结构,元素的文 ...
- jQuery中的text(),html(),val()的区别
一.jquery中HTML 1. 无参html() 方法用来获取任意元素的HTML内容,如果你调用多个选定元素的.html()方法,那么其读取的只是第一个元素,换句话说:如果选择器匹配多于一个的元素, ...
- 『jQuery』.html(),.text()和.val()的概述及使用--2015-08-11
如何使用jQuery中的.html(),.text()和.val()三种方法,用于读取,修改元素的html结构,元素的文本内容,以及表单元素的value值的方法 本节内容主要介绍的是如何使用jQu ...
- 我的JQuery复习笔记之①——text(),html(),val()的区别
text():①可匹配多个元素 ②过滤其中的标签(只显示文字) ③只适用于双标签 html():①只匹配选中元素中的第一个 ②不过滤其中标签 ③只适用于双标签 val():①只匹配选中元素中的第一个 ...
随机推荐
- 实战Apache+Tomcat集群和负载均衡
实战Apache+Tomcat集群和负载均衡 目录 1. 什么是J2EE集群... 3 1.1. 序言... 3 1.2. 基本术语... 3 伸缩性(Scalability): ...
- [转]Jackson 解析json数据之忽略解析字段注解@JsonIgnoreProperties
以前解析json用的惯的就是Google的gson了,用惯了基本就用它了,一直也没发现什么大问题,因为都是解析简单的json数据.但是最近学习springboot,要解析一个比较复杂的json数据.就 ...
- python标准库介绍——36 popen2 模块详解
==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...
- 转:ECharts图表组件入门教程之Theme:ECharts图表的皮肤是什么?如何给图表换主题(皮肤)Theme?
一.什么是ECharts图表的皮肤(主题)? 针对这个问题我只能这样回答,ECharts图表的主题(皮肤)就犹如人的衣服一样,是用来衬托和渲染主体,使其变得更加美观好看的目的.你去过ECharts图表 ...
- windows系统如何通过Xshell 客户端连接 linux系统(主要介绍ubuntu系统)
一. 1.查看ubuntu系统的ip地址:ifconfig 在window系统运行窗口下:ping ubuntu系统的IP地址:例如:ping 192.168.163.129 出现下述命令就是ping ...
- word中公式居中编号在最右端
1 显示标尺 2 设置居中制表符 3 在对应位置做标记 首先让公式居中 使用居中制表符在标尺的灰色地带标记位置,1)标记公式位置,鼠标左击 2)标记右边标号位置,注意和右边留有一定边距 4 公式左对齐 ...
- PHP的生成器、yield和协程
虽然之前就接触了PHP的yield关键字和与之对应的生成器,但是一直没有场景去使用它,就一直没有对它上心的研究.不过公司的框架是基于php的协程实现,觉得有必要深入的瞅瞅了. 由于之前对于生成器接触不 ...
- 初学FPGA
刚开始感觉FPGA不过也就是和51,ARM单片机那样写写程序就完事了,现在看来根本不是那么回事.从夏宇闻老师的Verilog HDL,黑金教程开始学起,但是感觉看到黑金时序篇时感觉少点什么,原来是缺少 ...
- 解决myeclipse/eclipse创建或导入maven工程时引发的问题
起因: 最近学习maven,按照教程把命令行创建的maven工程导入到eclipse/myeclipse,由于库中没有一些依赖包,所以在导入工程的时候开发工具自动下载依赖包.可是,由于天朝特殊环境的问 ...
- 高级数据库及一步一步搭建versant数据库
总的来说,高级数据库课程分为分布式数据库和面向对象数据库两块.分布式数据库介绍了分布式数据库的方方面面,包括数据库系统的设计.查询处理优化.事务管理和恢复.并发控制.可靠性.安全性与目录管理等.面向对 ...