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">火爆銷售中</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>效果:

元素相對(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">火爆銷售中</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>效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JQuery中DOM節(jié)點(diǎn)的操作與訪問(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 實(shí)時(shí)保存頁(yè)面動(dòng)態(tài)添加的數(shù)據(jù)的示例
本篇文章主要介紹了jQuery 實(shí)時(shí)保存頁(yè)面動(dòng)態(tài)添加的數(shù)據(jù)的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
自寫(xiě)的jQuery異步加載數(shù)據(jù)添加事件
這篇文章主要介紹一個(gè)自寫(xiě)的jQuery異步加載數(shù)據(jù)添加事件的方法,需要的朋友可以參考下2014-05-05
jquery.validate表單驗(yàn)證插件使用詳解
這篇文章主要為大家詳細(xì)介紹了jquery.validate表單驗(yàn)證插件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
jquery判斷對(duì)象是否為空并遍歷對(duì)象的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇jquery判斷對(duì)象是否為空并遍歷對(duì)象的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
全面解析jQuery中的$(window)與$(document)的用法區(qū)別
這篇文章主要介紹了jQuery中的$(window)與$(document)的用法區(qū)別,具體內(nèi)容大家可查看下文的詳細(xì)講解,感興趣的小伙伴們可以參考一下。2017-08-08

