值操作是对DOM属性value进行读取和设置操作。 比如html()、 text()、 val().

1. html

1. 1 html()获取值

返回值:String

描述:获取集合中第一个匹配元素的HTML内容

格式:

$(selector).html()

这个方法不接受任何元素

作用:在一个HTML文档中, 可以使用.html()方法来获取任意一个元素的内容。 如果选择器匹配多个元素, 那么只有第一个匹配元素的HTML内容会被获取。

实例代码:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>html Demo</title>
<script type="text/javascript" src="jquery.js"></script>
<script>
$("div.demo-container").html();
</script>
</head>
<body>
<div class="demo-container">
<div class="demo-box">Demonstration Box</div>
</div>
</body>
</html>

执行结果为:

Demonstration Box

1.2 html() 设置值

格式:

  $(selector).html(htmlString);

  返回值:jQuery

  作用: 用来设置每个匹配元素的一个HTML字符串

  htmlString 类型:string

代码示例:

  

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>html Demo</title>
<style type="text/css">
.red{
coler: red;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function () {
$("div").html("<span class='red'>Hello <b>Again</b></span>");
})
</script>
</head>
<body>
<span>Hello</span>
<div></div>
<div></div>
<div></div>
</body>
</html>

执行结果:

2.   text()

2.1  使用text()获取值

语法:

$("selector").text()

返回值:String

作用:得到匹配元素集合中每个元素的合并文本, 包括他们的后代

示例代码:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>text Demo</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function () {
$("div.demo-container").text()
})
</script>
</head>
<body>
<div class="demo-container">
<div class="demo-box">Demonstration Box</div>
<ul>
<li>list item 1</li>
<li>list <strong>item</strong> 2</li>
</ul>
</div>
</body>
</html>

执行结果为:

注意:

 .text()方法不能使用在input元素或scripts元素上。

 .input 或 textarea 需要使用.val()方法获取或者设置文本值。

 得到scripts元素的值, 会使用.html()方法。

2.2 使用Text() 设置文本内容

格式:

$(selector).text(text)

返回值:jQuery

text(参数类型): String 或 Number 或 Boolean

作用:用于设置匹配元素内容的文本。当提供的是一个Number或者Boolean的时候,那么将被转换成一个String表现形式, 提供给这个方法

代码示例:

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>text Demo</title>
<style type="text/css">
p{
color: blue;
margin: 8px;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function () {
$("p").text("<b>Some</b> new text.")
})
</script>
</head>
<body>
<p>Test Paragraph.</p>
</body>
</html>

执行结果为:

3. val()

3.1 使用val()方法获取值

格式:

$(selector).vall();

返回值:String

参数: 不接收任何参数

作用:获取匹配的元素集合中第一个元素的当前值

.val()方法主要用于获取表单元素的值, 如input, select 和 textarea。

当在一个空集合上调用, 它返回undefined。

当该集合中的第一个元素是一个select-multiple(即, select元素设置了multiple属性), .val()返回一个包含每个选择项值的数组。

对于选择框(select), 复选框(checkbox)和单选按钮(radio button), 也可以使用 :selected 和checked选择器来获取值。例如:

//从一个下拉选择框中获取值

$("select.foo option: selected").val();

// 简单的从一个下拉选择框中获取值

$("select.foo").val();

//Get the value form a checked checkbox

$("input: checkbox: checked).val();

//Get the value from a set of radio buttons

$("input: radio[name=bar]: checked).val();

注意: 通过.val()方法从<textarea>元素中获取的值是不含有回车(\r)字符的。

示例代码:

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>val Demo</title>
<style type="text/css">
p{
color: blue;
margin: 8px;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(function () {
$("input").keyup(function () {
var value = $(this).val();
$("p").text(value);
});
})
</script>
</head>
<body>
<input type="text" value="some text"></input>
<p></p>
</body>
</html>

执行结果为:

3.2 使用val()设置值

格式:

$(selector).val(value);

返回值: jQuery

参数(value):String 或 Number 或 Array

作用:一个String , Number 或 一个以字符串形式的数组来设定每个匹配元素的值。

此方法通常用于设置表单字段的值。

val()允许你传递一个元素值的数组。 当使用在包含像<input type='checkbox">, <input type="radio>和<select>中的<option>元素的jQuery对象上的时候是非常有用的。在这种情况下, input和option和value与数组元素相匹配的情况下将被选中(checked)或选定(selencted) ,而那些与数组元素值不匹配的value是未选中(unchecked)或未被选(unselected), 这取决于元素类型。

对于<input type="radio"> 属于一个单选按钮组, 还有<select>的其他元素都将被取消选中。

使用这个方法设置值, 不会触发change事件。为此, 相关的时间处理程序不会被执行。如果要执行他们, 应该在设置值之后调用.trigger("change")

代码示例:

点击按钮时, 在文本框中显示按钮的值。

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>val Demo</title>
<style type="text/css">
button{
margin: 4px;
cursor: pointer;
}
input{
margin: 4px;
color: blue;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(function () {
$("button").click(function () {
var text = $(this).text();
$("input").val(text);
});
})
</script>
</head>
<body>
<div>
<button>Feed</button>
<button>the</button>
<button>Input</button>
</div>
<input type="text" value="click a button">
</body>
</html>

执行结果为:

jQuery属性操作之值操作的更多相关文章

  1. jQuery 效果函数,jquery文档操作,jQuery属性操作方法,jQuerycss操作函数,jQuery参考手册-事件,jQuery选择器

    jQuery 效果函数 方法 描述 animate() 对被选元素应用“自定义”的动画 clearQueue() 对被选元素移除所有排队的函数(仍未运行的) delay() 对被选元素的所有排队函数( ...

  2. jQuery属性遍历、HTML操作

    jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery 遍历函数 jQuery 遍历函数包括了用于筛选.查找和串联元素的方法.    .add() 将元素添加到匹配元素的集合中. . ...

  3. jQuery源代码学习之八——jQuery属性操作模块

    一.jQuery属性模块整体介绍 jQuery的属性操作模块分四个部分:html属性操作,dom属性操作,类样式操作,和值操作. html属性操作(setAttribute/getAttribute) ...

  4. python全栈开发day48-jqurey自定义动画,jQuery属性操作,jQuery的文档操作,jQuery中的ajax

    一.昨日内容回顾 1.jQuery初识 1).使用jQuery而非JS的六大理由 2).jQuery对象和js对象转换 3).jQuery的两大特点 4).jQuery的入口函数三大写法 5).jQu ...

  5. Jquery框架1.选择器|效果图|属性、文档操作

    1.JavaScript和jquery的对比 书写繁琐,代码量大 代码复杂 动画效果,很难实现.使用定时器 各种操作和处理 <!DOCTYPE html> <html lang=&q ...

  6. 前端开发之jQuery属性和文档操作

    主要内容: 1.jQuery属性操作 2.jQuery文档操作 一.jQuery属性操作 1.什么是jQuery的属性操作? jQuery的属性操作模块包括四个部分:html属性操作,dom属性操作, ...

  7. jQuery属性操作

    jQuery 的属性操作的核心部分其实就是对底层 getAttribute().setAttributes()等方法的一系列兼容性处理 ...if ( notxml ) { name = name.t ...

  8. jQuery 属性操作和CSS 操作

    如有在jQuery方法中涉及到函数,此函数必定会返回一个数值(函数由于运行次数不同触发一些不同效果) jQuery 属性操作方法(以下方法前些日子学习过,不再赘述) addClass() attr() ...

  9. JQuery DOM操作 、属性和CSS样式操作、其他函数

    DOM操作 1.在div1内部最后追加一个节点 $("#div1").append("<img src='../01-HTML基本标签/img/Male.gif'/ ...

随机推荐

  1. Make It One CodeForces - 1043F (数论,最短路,好题)

    大意: 给定序列$a$, 求最小子集, 使得gcd为1. 对于数$x$, 素因子多少次幂是无关紧要的, 这样就可以用一个二进制数来表示. $x$取$gcd$后的二进制状态最多$2^7$, 可以暴力枚举 ...

  2. 深入理解计算机系统 第十章 系统级I/O 第二遍

    了解 Unix I/O 的好处 了解 Unix I/O 将帮助我们理解其他的系统概念 I/O 是系统操作不可或缺的一部分,因此,我们经常遇到 I/O 和其他系统概念之间的循环依赖.例如,I/O 在进程 ...

  3. Codeforces Round #406 (Div. 2) A MONSTER

    A. The Monster time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Docker 容器数据卷(Data Volume)与数据管理

    卷(Volume)是容器中的一个数据挂载点,卷可以绕过联合文件系统,从而为Docker 提供持久数据,所提供的数据还可以在宿主机-容器或多个容器之间共享.通过卷,我们可以可以使修改数据直接生效,而不必 ...

  5. Qt常用的登录界面设计

    记录一下Qt常用的登录界面的设计 方便以后使用! 1.QpushButton改变一个按钮的颜色,当鼠标放上去和移开时显示不同的颜色.QPushButton { background-color: rg ...

  6. 第十章、typing模块

    目录 第十章.typing模块 一.说明 二.typing模块的作用 三.使用typing模块 四.typing常用类型 第十章.typing模块 一.说明 二.typing模块的作用 类型检查,防止 ...

  7. 第十二章·Kibana深入-日志图形展示

    1.Kibana创建区域图 Kibana支持多重图从展示功能,需要日志是json格式的支持. Kibana区域图 打开浏览器,访问:http://10.0.0.54:5601   选择一个日志  ...

  8. S19格式

    S-record格式文件是Freescale CodeWarrior编译器生成的后缀名为.S19的程序文件,是一段直接烧写进MCU的ASCII码,英文全称问Motorola format for EE ...

  9. IDEA利用mybatis-generator自动生成dao和mapper

    pom.xml配置 <properties> <java.version>1.8</java.version> <mybatis-generator-core ...

  10. docker镜像pull不下来最终解决方法

    pull镜像wordpress下来,但是出现如下错误: # docker pull wordpress:latest Error response from daemon: Get https://r ...