小編給大家分享一下jQuery如何獲取操作元素的內(nèi)容和樣式,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
連城ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書(shū)銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書(shū)合作)期待與您的合作!
具體如下:
<html> <head> <title>jQuery操作元素的樣式和內(nèi)容</title> <meta charset="UTF-8"/> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> //操作元素的樣式 function testHtml1(){ //獲取要操作的對(duì)象 var showdiv=$("#showdiv"); //操作對(duì)象的內(nèi)容 alert(showdiv.html()); //我們獲得是對(duì)象中的內(nèi)容,沒(méi)有進(jìn)行HTML執(zhí)行的源代碼內(nèi)容,不論是標(biāo)簽還是內(nèi)容 } function testHtml2(){ //獲取要操作的對(duì)象 var showdiv=$("#showdiv"); //在對(duì)象中進(jìn)行元素添加操作 showdiv.html("<b>clannad 賽高!</b>"); //會(huì)對(duì)HTML標(biāo)簽進(jìn)行解析執(zhí)行 } function testHtml3(){ //獲取要操作的對(duì)象 var showdiv=$("#showdiv"); //在對(duì)象中進(jìn)行元素拼接操作 showdiv.html(showdiv.html()+"<b>clannad 賽高!</b>"); //可以進(jìn)行字符的拼接,其中showdiv的返回值是一個(gè)字符串,我們利用+進(jìn)行拼接 } function testText1(){ //獲取要操作的對(duì)象 var showdiv=$("#showdiv"); //在對(duì)象中進(jìn)行元素添加操作 alert(showdiv.text()); //顯示的結(jié)果不會(huì)包含標(biāo)簽 } function testText2(){ //獲取要操作的對(duì)象 var showdiv=$("#showdiv"); //在對(duì)象中進(jìn)行元素添加操作 showdiv.text("<b>古河渚 賽高!</b>"); //會(huì)把整個(gè)文本內(nèi)容寫(xiě)入div,不會(huì)解析標(biāo)簽 } //操作元素的樣式 function testCss1(){ //獲取對(duì)象 var showdiv=$("#showdiv"); //添加樣式 showdiv.css("background-color","yellow"); //獲取對(duì)象的對(duì)應(yīng)元素值 alert(showdiv.css("width")); //返回輸入屬性的值 } function testCss2(){ //獲取對(duì)象 var showdiv=$("#show2"); //添加樣式 showdiv.css({"background-color":"purple","border":"solid 1px"}); //我們利用{}把多個(gè)屬性括起來(lái)一次設(shè)置幾種元素樣式,不同屬性之間用,分割,元素的賦值用: } function testAddClass(){ //獲取對(duì)象 var div=$("#show2"); //添加一個(gè)類屬性 div.addClass("common"); //疊加類屬性 div.addClass("word"); //一個(gè)對(duì)象可以添加多個(gè)類屬性,注:如果原對(duì)象含有這個(gè)屬性,類屬性的值不會(huì)將其覆蓋 } function testRemoveClass(){ //獲取對(duì)象 var div=$("#show2"); //添加一個(gè)類屬性 div.remove("word"); //移除對(duì)象的一個(gè)類屬性 } </script> <style type="text/css"> #showdiv{ width: 300px; height: 300px; border: solid 1px; /*background-color: yellow;*/ } #show2{ width: 300px; height: 300px; /*border: solid 1px yellow;*/ /*background-color: purple;*/ } .common{ width: 300px; height: 300px; border: solid 2px yellow; background-color: red; } .word{ font-size: 40px; font-size: bold; } </style> </head> <body> <h4>jQuery操作元素的樣式和內(nèi)容</h4> <hr /> <input type="button" name="" id="" value="測(cè)試對(duì)象內(nèi)容-html" onclick="testHtml1()"/> <input type="button" name="" id="" value="測(cè)試對(duì)象添加內(nèi)容-html" onclick="testHtml2()"/> <input type="button" name="" id="" value="測(cè)試對(duì)象拼接內(nèi)容-html" onclick="testHtml3()"/> <input type="button" name="" id="" value="測(cè)試對(duì)象內(nèi)容-text" onclick="testText1()"/> <input type="button" name="" id="" value="測(cè)試對(duì)象添加內(nèi)容-text" onclick="testText2()"/> <hr /> <input type="button" name="" id="" value="測(cè)試對(duì)象樣式" onclick="testCss1()"/> <input type="button" name="" id="" value="測(cè)試對(duì)象樣式---json" onclick="testCss2()"/> <input type="button" name="" id="" value="測(cè)試對(duì)象添加類樣式" onclick="testAddClass()"/> <input type="button" name="" id="" value="測(cè)試對(duì)象移除類樣式" onclick="testRemoveClass()"/> <hr /> <div id="showdiv"> <b>古河渚 賽高!</b> </div> <div id="show2"> Clannad After Story </div> </body> </html>
以上是“jQuery如何獲取操作元素的內(nèi)容和樣式”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前標(biāo)題:jQuery如何獲取操作元素的內(nèi)容和樣式
本文URL:http://chinadenli.net/article18/pooddp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、營(yíng)銷型網(wǎng)站建設(shè)、企業(yè)建站、面包屑導(dǎo)航、用戶體驗(yàn)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)