jQuery中的CSS-DOM操作
除了css()以外,還有獲取和設(shè)置元素高度、寬度、相對位置等的樣式操作方法,語法如下:
高度和寬度示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS-DOM操作演示示例</title> <style> .hot{ color: #ff0000; } a{ color: #000000; text-decoration: none; } </style> <!--引入jQuery--> <script src="../jquery-3.3.1.js"></script> <!--javascript--> <script> $(function(){ // 設(shè)置height()和width()都是200 $("li").find("span").css({"display":"block","border":"1px solid red"}).width(200).height(200); }); </script> </head> <body> <img src="ad.jpg" alt="1" /> <img src="ad.jpg" alt="2" /> <img src="ad.jpg" alt="3" /> <ul> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >小米的MI 2手機(jī)</a><span class="hot">火爆銷售中</span></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >蘋果iPad mini</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >三星GALAXY III</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >蘋果iPhone 5s</a></li> </ul> <input type="button" value="點(diǎn)擊按鈕" /> </body> </html>
效果:
元素相對位置示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS-DOM操作演示示例</title> <style> .hot{ color: #ff0000; } a{ color: #000000; text-decoration: none; } </style> <!--引入jQuery--> <script src="../jquery-3.3.1.js"></script> <!--javascript--> <script> $(function(){ // 設(shè)置height()和width()都是200 //$("li").find("span").css({"display":"block","border":"1px solid red"}).width(200).height(200); //offset $("input[type='button']").click(function(){ var x=$("span").offset(); console.log(x); $("span").text("橫坐標(biāo):"+x.left+",縱坐標(biāo):"+x.top); }); }); </script> </head> <body> <img src="ad.jpg" alt="1" /> <img src="ad.jpg" alt="2" /> <img src="ad.jpg" alt="3" /> <ul> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >小米的MI 2手機(jī)</a><span class="hot">火爆銷售中</span></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >蘋果iPad mini</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >三星GALAXY III</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >蘋果iPhone 5s</a></li> </ul> <input type="button" value="點(diǎn)擊按鈕" /> </body> </html>
效果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery滿屏焦點(diǎn)圖左右滾動(dòng)特效代碼分享
這篇文章主要介紹了jQuery滿屏焦點(diǎn)圖左右滾動(dòng)特效,一段精致的焦點(diǎn)圖輪播代碼,有需要的小伙伴可以參考下。2015-09-09JQuery 插件制作實(shí)踐 xMarquee插件V1.0
今天要介紹的是實(shí)現(xiàn)類跑馬燈效果的的廣告插件。類似偶公司web-dev的同事在網(wǎng)站首頁上做的目錄廣告播放器。其實(shí)很簡單,LEVIN實(shí)際應(yīng)用中也用到,所以稍作整理如下.2010-04-04jQuery實(shí)現(xiàn)動(dòng)態(tài)向上滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)動(dòng)態(tài)向上滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12基于jquery插件編寫countdown計(jì)時(shí)器
這篇文章主要為大家詳細(xì)介紹了jquery插件編寫countdown計(jì)時(shí)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06jquery實(shí)現(xiàn)簡單Tab切換菜單效果
這篇文章主要介紹了jquery實(shí)現(xiàn)簡單Tab切換菜單效果的相關(guān)資料,需要的朋友可以參考下2016-06-06解決jQuery使用JSONP時(shí)產(chǎn)生的錯(cuò)誤
這篇文章主要介紹了解決jQuery使用JSONP時(shí)產(chǎn)生的錯(cuò)誤,需要的朋友可以參考下2015-12-12jquery.validate表單驗(yàn)證插件使用方法解析
這篇文章主要為大家詳細(xì)介紹了jquery.validate表單驗(yàn)證插件使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11