jQuery | position()及其示例

position()方法是jQuery中的一个内置方法,用于查找第一个匹配元素在DOM树中相对于其父元素的位置。

null

语法:

$(selector).position()

参数: 此方法不包含任何参数。 返回值: 此方法返回第一个匹配元素的位置。 下面的示例演示了jQuery中的position()方法: 例子:

html

<!DOCTYPE html>
< html >
< head >
< title >The position Method</ title >
< script src =
</ script >
<!-- jQuery code to show the working of this method -->
< script >
$(document).ready(function(){
$("button").click(function(){
var x = $("p").position();
alert("Top position: " + x.top);
});
});
</ script >
< style >
div {
width: 350px;
height: 100px;
font-weight: bold;
padding:20px;
font-size: 25px;
border: 2px solid green;
}
</ style >
</ head >
< body >
< div >
< p >GeeksforGeeks.</ p >
<!-- Click on this button  -->
< button >Click Here!</ button >
</ div >
</ body >
</ html >


输出: 单击按钮之前:

图片[1]-jQuery | position()及其示例-yiteyi-C++库

点击按钮后:

图片[2]-jQuery | position()及其示例-yiteyi-C++库

相关文章:

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享