jQuery中的CSS-DOM操作
除了css()以外,還有獲取和設(shè)置元素高度、寬度、相對(duì)位置等的樣式操作方法,語(yǔ)法如下:

高度和寬度示例:
<!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">火爆銷(xiāo)售中</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" >蘋(píng)果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" >蘋(píng)果iPhone 5s</a></li>
</ul>
<input type="button" value="點(diǎn)擊按鈕" />
</body>
</html>效果:

元素相對(duì)位置示例:
<!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">火爆銷(xiāo)售中</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" >蘋(píng)果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" >蘋(píng)果iPhone 5s</a></li>
</ul>
<input type="button" value="點(diǎn)擊按鈕" />
</body>
</html>效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JQuery中DOM節(jié)點(diǎn)的操作與訪(fǎng)問(wèn)方法實(shí)例分析
- jQuery中DOM常見(jiàn)操作實(shí)例小結(jié)
- jQuery中DOM操作原則實(shí)例分析
- JQueryDOM之樣式操作
- jQuery常見(jiàn)的遍歷DOM操作詳解
- jQuery中圖片展示插件highslide.js的簡(jiǎn)單dom
- jQuery Dom元素操作技巧
- jQuery中 DOM節(jié)點(diǎn)操作方法大全
- JQuery 選擇器、DOM節(jié)點(diǎn)操作練習(xí)實(shí)例
- jQuery DOM節(jié)點(diǎn)的遍歷方法小結(jié)
相關(guān)文章
jQuery滿(mǎn)屏焦點(diǎn)圖左右滾動(dòng)特效代碼分享
這篇文章主要介紹了jQuery滿(mǎn)屏焦點(diǎn)圖左右滾動(dòng)特效,一段精致的焦點(diǎn)圖輪播代碼,有需要的小伙伴可以參考下。2015-09-09
JQuery 插件制作實(shí)踐 xMarquee插件V1.0
今天要介紹的是實(shí)現(xiàn)類(lèi)跑馬燈效果的的廣告插件。類(lèi)似偶公司web-dev的同事在網(wǎng)站首頁(yè)上做的目錄廣告播放器。其實(shí)很簡(jiǎn)單,LEVIN實(shí)際應(yīng)用中也用到,所以稍作整理如下.2010-04-04
jQuery實(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插件編寫(xiě)countdown計(jì)時(shí)器
這篇文章主要為大家詳細(xì)介紹了jquery插件編寫(xiě)countdown計(jì)時(shí)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
jquery實(shí)現(xiàn)簡(jiǎn)單Tab切換菜單效果
這篇文章主要介紹了jquery實(shí)現(xiàn)簡(jiǎn)單Tab切換菜單效果的相關(guān)資料,需要的朋友可以參考下2016-06-06
解決jQuery使用JSONP時(shí)產(chǎn)生的錯(cuò)誤
這篇文章主要介紹了解決jQuery使用JSONP時(shí)產(chǎn)生的錯(cuò)誤,需要的朋友可以參考下2015-12-12
jquery.validate表單驗(yàn)證插件使用方法解析
這篇文章主要為大家詳細(xì)介紹了jquery.validate表單驗(yàn)證插件使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

