當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JQuery position()用法及代碼示例

position() 方法是 jQuery 中的內置方法,用於查找 DOM 樹中第一個匹配元素相對於其父元素的位置。

用法:

$(selector).position()

參數:該方法不包含任何參數。

返回值:該方法返回第一個匹配元素的位置。

例子:在此示例中,我們使用 jQuery position() 方法。

html


<!DOCTYPE html>
<html>
<head>
    <title>The position Method</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                let x = $("p").position();
                alert("Top position: " + x.top);
            });
        });
    </script>
    <style>
        body {
            display: flex;
            justify-content: center;
        }
        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>

輸出:
jquery-20
相關文章:



相關用法


注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 jQuery position() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。