詳解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ù)由兩個參數(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è)置單個屬性 $("button").click(function(){ $("#w3s").attr({ "href" : "http://www.w3school.com.cn/jquery", "title" : "W3School jQuery Tutorial" }); });//設(shè)置多個屬性
$("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)容和屬性詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
jquery網(wǎng)頁回到頂部效果(圖標(biāo)漸隱,自寫)
當(dāng)網(wǎng)頁內(nèi)容草雞多的時候,用戶就需要有個按鈕快速回到頂部,于是用js來實現(xiàn)下,畫布多說,直接上代碼2014-06-06JQuery實現(xiàn)動態(tài)表格點擊按鈕表格增加一行
動態(tài)表格,功能為點擊添加按鈕,表格增加一行并給其name屬性賦予的值,點擊刪除,自動刪除這一行,具體實現(xiàn)如下2014-08-08jQuery插件ajaxfileupload.js實現(xiàn)上傳文件
這篇文章主要為大家詳細介紹了jQuery插件ajaxfileupload.js實現(xiàn)上傳文件的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05