推扬网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
推扬网 门户 经验分享 查看内容

jQuery中offsetParent()方法用法实例

2020-4-11 13:59| 发布者: admin| 查看: 449| 评论: 0

这篇文章主要介绍了jQuery中offsetParent()方法用法,实例分析了offsetParent()方法的功能、定义及返回匹配元素所有祖先元素中第一个采用定位的祖先元素时的使用技巧,需要的朋友可以参考下

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

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

语法结构:
$(selector).offsetParent()

代码实例:


<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="//www.jb51.net/" />
<title>offsetParent()函数-推扬网www.tuiyang.com</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/


鲜花

握手

雷人

路过

鸡蛋

最新评论

精选推荐

    广告服务|投稿要求|禁言标准|版权说明|免责声明|手机版|小黑屋|推扬网 ( 粤ICP备18134897号 )|网站地图 | 邮箱:vayae@hotmail.com

    GMT+8, 2025-6-17 06:17 , Processed in 0.073023 second(s), 29 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    返回顶部