詳解jQuery設(shè)置內(nèi)容和屬性
一、設(shè)置內(nèi)容以及回調(diào)函數(shù)
方法
- text() - 設(shè)置或返回所選元素的文本內(nèi)容
- html() - 設(shè)置或返回所選元素的內(nèi)容(包括 HTML 標(biāo)記)
- val() - 設(shè)置或返回表單字段的值
例子
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});//設(shè)置文本內(nèi)容
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});//設(shè)置元素內(nèi)容
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});//設(shè)置值
$("#btn1").click(function(){
$("#test1").text(function(i,origText){
return "Old text: " + origText + " New text: Hello world!
(index: " + i + ")";
});
});//返回文本內(nèi)容
$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "Old html: " + origText + " New html: Hello <b>world!</b>
(index: " + i + ")";
});
});//返回元素內(nèi)容
TIPS
回調(diào)函數(shù)由兩個(gè)參數(shù):被選元素列表中當(dāng)前元素的下標(biāo),以及原始(舊的)值。然后以函數(shù)新值返回您希望使用的字符串。
二、設(shè)置屬性以及回調(diào)函數(shù)
方法
attr() - 設(shè)置屬性值
例子
$("button").click(function(){
$("#w3s").attr("href","http://www.w3school.com.cn/jquery");
});//設(shè)置單個(gè)屬性
$("button").click(function(){
$("#w3s").attr({
"href" : "http://www.w3school.com.cn/jquery",
"title" : "W3School jQuery Tutorial"
});
});//設(shè)置多個(gè)屬性
$("button").click(function(){
$("#w3s").attr("href", function(i,origValue){
return origValue + "/jquery";
});
});//回調(diào)函數(shù)
總代碼
<!doctype html>
<head>
<title>jq設(shè)置內(nèi)容和屬性</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#bt1").click(function(){
$("#p1").text("hello world1!");
});
$("#bt2").click(function(){
$("#p2").html("<h1>hello world2!</h1>");
});
$("#bt3").click(function(){
$("#t1").val("hello world3!");
});
$("#bt4").click(function(){
$("#p1").text(function(i,origText){
return "舊文本:"+origText+"新文本:這是新文本(index:"+i+")"
});
});
$("#bt5").click(function(){
$("#p2").html(function(i,origText){
return "舊HTML:"+origText+"新HTML:<b>這是新HTML</b>(index:"+i+")"
});
});
$("#bt6").click(function(){
$("#t1").val(function(i,origText){
return "舊值:"+origText+"新值:這是新值(index:"+i+")"
});
});
$("#bt7").click(function(){
$("a").attr("href","https://tieba.baidu.com");
});
$("#bt8").click(function(){
$("a").attr("href",function(i,origValue){return origValue + "/s?wd=1&rsv_spt=1&rsv_iqid=0xbc3b96600002e5f4&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=2&rsv_sug1=1&rsv_sug7=100&rsv_sug2=0&inputT=163&rsv_sug4=1686"});
});
});
</script>
</head>
<body>
<input type="button" id="bt1" value="設(shè)置文本" "">
<input type="button" id="bt2" value="設(shè)置HTML" "">
<input type="button" id="bt3" value="設(shè)置值" "">
<p id="p1">這是文本</p>
<p id="p2">這是HTML</p>
<p>這是:<input type="text" value="值" id="t1" style ="width:500px"></p>
<input type="button" id="bt4" value="顯示舊/新文本" "">
<input type="button" id="bt5" value="顯示舊/新HTML" "">
<input type="button" id="bt6" value="顯示舊/新值" "">
<hr><!--以上是設(shè)置內(nèi)容,一下是設(shè)置屬性-->
<input type="button" id="bt7" value="改變href值為貼吧" "">
<p><a id="baidu" rel="external nofollow" target="_blank">百度</a></p>
<input type="button" id="bt8" value="改變href值為搜索1(回調(diào)函數(shù))" "">
</body>
</html>
以上所述是小編給大家介紹的jQuery設(shè)置內(nèi)容和屬性詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
擴(kuò)展easyui.datagrid,添加數(shù)據(jù)loading遮罩效果代碼
easyui可以說是輕量級的前端UI框架,更新到1.2.1支持更多的事件,方法和屬性2010-11-11
jquery post方式傳遞多個(gè)參數(shù)值后臺以數(shù)組的方式進(jìn)行接收
在用jquery的post方式傳遞多個(gè)值時(shí),在后臺頁面可以用數(shù)組形式接收,很不錯(cuò)吧,可以接收多個(gè)值啊,接下來介紹實(shí)現(xiàn)方法,感興趣的朋友可以了解下哦2013-01-01
jquery nth-child()選擇器的簡單應(yīng)用
今天項(xiàng)目中遇到過一個(gè)這樣的問題,就是希望讀出來的文章列表能夠每隔五個(gè)加一個(gè)分割條,而不是每個(gè)都加。2010-07-07
Jquery Uploadify上傳帶進(jìn)度條的簡單實(shí)例
本篇文章主要是對Jquery Uploadify上傳帶進(jìn)度條的簡單實(shí)例進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
再說AutoComplete自動補(bǔ)全之實(shí)現(xiàn)原理
再說AutoComplete自動補(bǔ)全之實(shí)現(xiàn)原理,這里以asp.net后臺為主,考慮到性能最好是生成xml并入緩存。2011-11-11
jQuery實(shí)現(xiàn)動態(tài)控制頁面元素的方法分析
這篇文章主要介紹了jQuery實(shí)現(xiàn)動態(tài)控制頁面元素的方法,結(jié)合實(shí)例形式分析了jQuery事件響應(yīng)及頁面元素動態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12

