jquery offsetParent() method


  Translation results:

offset

UK[ˈɒfset] US[ˈɔ:fset]

vt. Offset; compensate; (for the purpose of comparison) to juxtapose (or Juxtapose); install a branch for (pipeline, etc.)

vi. form a branch, grow a branch; install a branch

n. start; set off; lithography; offset, compensate

adj. Branch; off-centre; offset; beginning

parent

English [ˈpeərənt] US [ˈperənt]

n.Father (or mother); ancestor; root, origin; protector

jquery offsetParent() methodsyntax

Function: The offsetParent() method returns the nearest ancestor positioned element. A positioned element is an element whose CSS position property is set to relative, absolute, or fixed. Position can be set through jQuery, or through the CSS position property.

Syntax: $(selector).offsetParent()

jquery offsetParent() methodexample

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("p").offsetParent().css("background-color","red");
  });
});
</script>
</head>
<body>
<div style="width:70%;position:absolute;left:100px;top:100px">
<div style="margin:50px;background-color:yellow">
<p>点击下面的按钮可以设置本段落的最近的父(祖先)元素的背景色。</p>
<div>
</div>
<button>点击这里</button>
</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance

Home

Videos

Q&A