DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jQuery中offsetParent()方法用法實例教程
jQuery中offsetParent()方法用法實例教程
編輯:JQuery特效代碼     

本文實例講述了jQuery中offsetParent()方法用法。分享給大家供大家參考。具體分析如下:

此方法可以返回匹配元素所有祖先元素中第一個采用定位的祖先元素。
所謂采用定位的父元素就是施加position:relative或者position:absolute(fixed)的元素。
此方法僅對可見元素有效。

語法結構:
代碼如下:$(selector).offsetParent()

代碼實例:

代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.cnblogs.com/" />
<title>offsetParent()函數-博客園</title>
<style type="text/css">
.grandfather
{
  width:200px;
  height:200px;
  background-color:red;
  position:relative;
}
.father
{
  width:150px;
  height:150px;
  background-color:blue;
}
.children
{
  width:50px;
  height:50px;
  background-color:green;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $(".children").offsetParent().css("backgroundColor","yellow");
  })
})
</script>
</head>
<body>
<div class="grandfather">
<div class="father">
  <div class="children"></div>
</div>
</div>
<button>查看效果</button>
</body>
</html>

以上代碼可以將children元素中所有祖先元素中,第一個采用定位的祖先元素的背景顏色設置為紅色。

希望本文所述對大家的jQuery程序設計有所幫助。

本文轉自:螞蟻部落http://www.softwhy.com/

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved