jQueryoffsetParent()方法

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

offsetParent()方法的定义和用法:

此方法可以返回匹配元素所有祖先元素中第一个采用定位的祖先元素。

所谓采用定位的父元素就是施加position:relative或者position:absolute(fixed)的元素。

此方法仅对可见元素有效。

语法结构:

$(selector).offsetParent()

代码实例:

<!DOCTYPE html><html><head><metacharset=" utf-8"><metaname="author"content="http://www.softwhy.com/"/><title>offsetParent()函数-蚂蚁部落</title><styletype="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><scriptsrc="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script><scripttype="text/javascript">$(document).ready(function(){

$("button").click(function(){

$(".children").offsetParent().css("backgroundColor","yellow");

})

})</script></head><body><divclass="grandfather"><divclass="father"><divclass="children"></div></div></div><button>查看效果</button></body></html>

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

原文地址如下:jQuery offsetParent()方法一章节。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航