jQuery - 设置内容和属性
来源:互联网 发布:手机通话记录软件大全 编辑:程序博客网 时间:2024/05/16 17:57
设置内容 - text()、html() 以及 val()
我们将使用前一章中的三个相同的方法来设置内容:
- text() - 设置或返回所选元素的文本内容
- html() - 设置或返回所选元素的内容(包括 HTML 标记)
- val() - 设置或返回表单字段的值
下面的例子演示如何通过 text()、html() 以及 val() 方法来设置内容:
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script><script>$(document).ready(function(){ $("#btn1").click(function(){ $("#test1").text("Hello world!"); }); $("#btn2").click(function(){ $("#test2").html("<b>Hello world!</b>"); }); $("#btn3").click(function(){ $("#test3").val("Dolly Duck"); });});</script></head><body><p id="test1">This is a paragraph.</p><p id="test2">This is another paragraph.</p><p>Input field: <input type="text" id="test3" value="Mickey Mouse"></p><button id="btn1">Set Text</button><button id="btn2">Set HTML</button><button id="btn3">Set Value</button></body></html>
text()、html() 以及 val() 的回调函数
上面的三个 jQuery 方法:text()、html() 以及 val(),同样拥有回调函数。回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。
下面的例子演示带有回调函数的 text() 和 html():
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script><script>$(document).ready(function(){ $("#btn1").click(function(){ $("#test1").text(function(i,origText){ return "Old text: " + origText + " New text: Hello world! (index: " + i + ")"; }); }); $("#btn2").click(function(){ $("#test2").html(function(i,origText){ return "Old html: " + origText + " New html: Hello <b>world!</b> (index: " + i + ")"; }); });});</script></head><body><p id="test1">This is a <b>bold</b> paragraph.</p><p id="test2">This is another <b>bold</b> paragraph.</p><button id="btn1">Show Old/New Text</button><button id="btn2">Show Old/New HTML</button></body></html>
设置属性 - attr()
jQuery attr() 方法也用于设置/改变属性值。
下面的例子演示如何改变(设置)链接中 href 属性的值:
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script><script>$(document).ready(function(){ $("button").click(function(){ $("#w3s").attr("href","http://www.w3cschool.cc/jquery"); });});</script></head><body><p><a href="http://www.w3cschool.cc" id="w3s">W3Cschool.cc</a></p><button>Change href Value</button><p>Mouse over the link (or click on it) to see that the value of the href attribute has changed.</p></body></html>
attr() 方法也允许您同时设置多个属性。
下面的例子演示如何同时设置 href 和 title 属性:
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script><script>$(document).ready(function(){ $("button").click(function(){ $("#w3s").attr({ "href" : "http://www.w3cschool.cc/jquery", "title" : "W3Cschool jQuery Tutorial" }); });});</script></head><body><p><a href="http://www.w3cschool.cc" id="w3s">W3Cschool.cc</a></p><button>Change href and title</button><p>Mouse over the link to see that the href attribute has changed and a title attribute is set.</p></body></html>
attr() 的回调函数
jQuery 方法 attr(),也提供回调函数。回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。
下面的例子演示带有回调函数的 attr() 方法:
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script><script>$(document).ready(function(){ $("button").click(function(){ $("#w3s").attr("href", function(i,origValue){ return origValue + "/jquery"; }); }); });</script></head><body><p><a href="http://www.w3cschool.cc" id="w3s">W3Cschool.cc</a></p><button>Change href Value</button><p>Mouse over the link (or click on it) to see that the value of the href attribute has changed.</p></body></html>
0 0
- jQuery - 设置内容和属性
- jQuery - 设置内容和属性
- jQuery - 设置内容和属性
- jQuery - 设置内容和属性
- jQuery - 设置内容和属性
- jQuery - 设置内容和属性
- jQuery设置内容和属性
- jQuery - 设置获取内容和属性
- 13. jQuery - 设置内容和属性
- jQuery--获取/设置内容和属性
- jQuery - 设置div的内容和属性
- jquery 设置内容和属性 元素添加和删除
- Jquery—获取和设置html内容和属性
- 我的第十三课:jQuery - 设置内容和属性
- jQuery学习之获取和设置内容以及属性
- jQuery 之 设置页面内容和属性(七)
- jQuery - 获得内容和属性
- jQuery - 获得内容和属性
- 日志
- java.util.ResourceBundle使用
- web.xml配置详解之filter
- HDU 1712 ACboy needs your help
- 在centOS 6.3下,进行hadoop 2.0.0-alpha(yarn)本地模式部署
- jQuery - 设置内容和属性
- [卢卡斯定理+中国剩余定理] hdu 5446 Unknown Treasure
- js打印对象 很有用的一个函数
- MySQL集群:主从数据库配置 实现查询负载
- 为什么很多年轻人总会感到迷茫
- MFC通过注册表 实现程序开机自动运行
- android 开发第六弹--View事件传递和碎片
- 使用Eclipse构建Maven的SpringMVC项目
- Java/Android 多少年月日时分秒前