当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery中offsetParent()方法用法实例教程

jQuery中offsetParent()方法用法实例教程

2019年03月30日  | 移动技术网IT编程  | 我要评论

本文实例讲述了jquery中offsetparent()方法用法。分享给大家供大家参考。具体分析如下:

此方法可以返回匹配元素所有祖先元素中第一个采用定位的祖先元素。
所谓采用定位的父元素就是施加position:relative或者position:absolute(fixed)的元素。
此方法仅对可见元素有效。

语法结构:

代码如下:

$(selector).offsetparent()

 

代码实例:

 

代码如下:


<!doctype html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="https://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>
<p class="grandfather">
<p class="father">
  <p class="children"></p>
</p>
</p>
<button>查看效果</button>
</body>
</html>

以上代码可以将children元素中所有祖先元素中,第一个采用定位的祖先元素的背景颜色设置为红色。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网