JQuery:JQuery设置HTML
JQuery:设置HTML
1、Query - 设置内容和属性
设置内容 - text()、html() 以及 val()
我们将使用前一章中的三个相同的方法来设置内容:
text() - 设置或返回所选元素的文本内容
html() - 设置或返回所选元素的内容(包括 HTML 标记)
val() - 设置或返回表单字段的值
下面的例子演示如何通过 text()、html() 以及 val() 方法来设置内容:
实例1:
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("RUNOOB");
});
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery的使用!!!</title>
<script src="jquery-3.1.0.js"></script> <script>
$(function(){
//设置文本
$("#btn1").click(function(){
$("#test1").text("hello world!");
}); //设置HTML
$("#btn2").click(function(){
$("#test2").html("<i>hello world!</i>")
}); //设置Value
$("#btn3").click(function(){
$("#test3").val("hello world!");
});
});
</script>
</head>
<body>
<p id="test1">真是一个段落</p>
<p id="test2">这是另外一个段落</p>
<p>Input:<input type="text" value="这是文本框值" id="test3"></p>
<button id="btn1">设置文本</button>
<button id="btn2">设置HTML</button>
<button id="btn3">设置Value</button>
</body>
</html>

text()、html() 以及 val() 的回调函数
上面的三个 jQuery 方法:text()、html() 以及 val(),同样拥有回调函数。回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。
下面的例子演示带有回调函数的 text() 和 html():
实例2:
$("#btn1").click(function(){
$("#test1").text(function(i,origText){
return "旧文本: " + origText + " 新文本: Hello world! (index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "旧 html: " + origText + " 新 html: Hello <b>world!</b> (index: " + i + ")";
});
});
代码如下:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>JQuery的使用!!!</title>
<script src="jquery-3.1.0.js"></script>
<script> $(function(){
//显示新/旧文本
$("#btn1").click(function(){
$("#test1").text(function(i,origText){
return "旧文本:"+ origText + " 新文本:Hello World!(index:" + i +")";
});
}); //显示新/旧HTML
$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "旧HTML:"+ origText + " <b>新HTML:Hello World!(index:" + i +")</b>";
});
});
}); </script>
</head>
<body>
<p id="test1">真是一个旧的段落</p>
<p id="test2">这是另外一个旧的段落</p>
<button id="btn1">显示新/旧文本</button>
<button id="btn2">显示新/旧HTML</button>
</body>
</html>

2、Query - 设置属性
jQuery attr() 方法也用于设置/改变属性值。
下面的例子演示如何改变(设置)链接中 href 属性的值:
实例1:
$("button").click(function(){
$("#runoob").attr("href","http://www.runoob.com/jquery");
});
代码如下:
attr() 方法也允许您同时设置多个属性。
下面的例子演示如何同时设置 href 和 title 属性:
实例2:
$("button").click(function(){
$("#runoob").attr({
"href" : "http://www.runoob.com/jquery",
"title" : "jQuery 教程"
});
});
代码如下:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>JQuery的使用!!!</title>
<script src="jquery-3.1.0.js"></script>
<script> $(function(){
//显示新/旧文本
$("button").click(function(){
alert("更换前:"+$("#test").attr("href"));
$("#test").attr("href","https://github.com");
alert("更换后:"+$("#test").attr("href"));
});
}); </script>
</head>
<body>
<p><a href="http://www.baidu.com" id="test">链接</a></p>
<button>点击按钮更换href链接</button>
</body>
</html>

3、attr() 的回调函数
jQuery 方法 attr(),也提供回调函数。回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。
下面的例子演示带有回调函数的 attr() 方法:
实例1:
$("button").click(function(){
$("#runoob").attr("href", function(i,origValue){
return origValue + "/jquery";
});
});
代码如下:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>JQuery的使用!!!</title>
<script src="jquery-3.1.0.js"></script>
<script> $(function(){
//显示新/旧文本
$("button").click(function(){
alert("更换前:"+$("#test").attr("href"));
$("#test").attr("href",function(i,orinHerf){
return orinHerf + "/xiayuanquan";
});
alert("更换后:"+$("#test").attr("href"));
});
}); </script>
</head>
<body>
<p><a href="http://www.baidu.com" id="test">链接</a></p>
<button>点击按钮更换href链接</button>
</body>
</html>

JQuery:JQuery设置HTML的更多相关文章
- jQuery中设置form表单中action的值的方法
下面介绍在jQuery中设置form表单中action的值的方法. $("#myFormId").attr("action", "userinfo.s ...
- jQuery中设置form表单中action值与js有什么不同。。。。
jQuery中设置form表单中action值与js有什么不同.... HTML代码如下: <form action="" method="post" i ...
- jquery 获取设置值、添加元素详解
jQuery 获取内容和属性 jQuery DOM 操作 jQuery 中非常重要的部分,就是操作 DOM 的能力. jQuery 提供一系列与 DOM 相关的方法,这使访问和操作元素和属性变得很容易 ...
- 转载JQuery 获取设置值,添加元素详解
转载原地址 http://www.cnblogs.com/0201zcr/p/4782476.html jQuery 获取内容和属性 jQuery DOM 操作 jQuery 中非常重要的部分,就是操 ...
- jQuery中设置form表单中action值的方法
jQuery中设置form表单中action值的方法 (2011-03-17 10:18:19) 转载▼ 标签: 杂谈 html代码: <form id="myFormId&quo ...
- jQuery扩展函数设置所有对象只读
jQuery(function ($) { $.fn.disable = function () { return this.each(func ...
- jquery datagrid设置pageSize不起作用
http://www.2cto.com/kf/201212/178098.html —————————————————————————————————————————————————————————— ...
- jQuery之设置元素内容(移动和复制元素,使用append(),appendTo()方法)
jQuery之设置元素内容(移动和复制元素,使用append(),appendTo()方法) ---------- 如果想把内容添加到现有内容末尾,可以利用append()命令.append()命令语 ...
- jquery datatable设置垂直滚动后,表头(th)错位问题
jquery datatable设置垂直滚动后,表头(th)错位问题 问题描述: 我在datatable里设置:"scrollY": '300px',垂直滚动属性后,表头的宽度就会 ...
- jquery获取设置服务器控件的方法
jquery获取设置服务器控件的方法 获取TextBox,HiddenField,Label的值.例如: <asp:TextBox ID="txtBoxID" runat=& ...
随机推荐
- [百科] - SIP(会话发起协议)
SIP(会话发起协议)SIP是类似于HTTP的基于文本的协议.SIP可以减少应用特别是高级应用的开发时间.由于基于IP协议的SIP利用了IP网络,固定网运营商也会逐渐认识到SIP技术对于他们的深远意义 ...
- 最基本的session保存法,类似于默认的files方法
关于session的几个补充函数 在PHP下,关于session的讨论很多,其实在PHP4中还有几个函数是我们平时没有注意到的. 下面我把它们介绍给大家吧. 其中的session_set_save_h ...
- 【iCore2双核心板视频教程二】iM_LAN 100M 以太网模块TCP通信例程
============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...
- ProtocalBuffers学习记录
Google Protocol Buffer 的使用和原理 Google Protocol Buffers 概述 Google Protocol Buffers 入门 Protocol Buffers ...
- artDialog ( v 6.0.2 ) content 参数引入页面 html 内容
/*! artDialog v6.0.2 | https://github.com/aui/artDialog */ 将页面某一隐藏的 div 的 html 内容传到 artdialog 的弹窗中,并 ...
- TCP移动端跟服务器数据交互
同一台笔记本下的客户端和服务端 TCPClient 客户端: // RootViewController.h#import <UIKit/UIKit.h>#import "As ...
- UIButton的遍历
for (id obj in self.view.subviews) { if ([obj isKindOfClass:[UIButton class]]) { ...
- egrep 查找IP
1. egrep '([^0-9]|\<)(([0-1]?[0-9]{0,2}|([2]([0-4][0-9]|[5][0-5])))\.){3}([0-1]?[0-9]{0,2}|([2]([ ...
- 11.PHP内核探索:嵌入式PHP PHP内核探索:嵌入式PHP
从PHP源码目录结构的介绍以及PHP生命周期可知:嵌入式PHP类似CLI,也是SAPI接口的另一种实现. 一般情况下,它的一个请求的生命周期也会和其它的SAPI一样:模块初始化=>请求初始化=& ...
- Bit-Value Type
https://dev.mysql.com/doc/refman/5.7/en/bit-type.html MySQL 5.7 Reference Manual / ... / Bit-Val ...