我正在嘗試獲取每個 $('.example p') 元素的每個 offset().top 值
console.log($('.example p')[1].offset().top)
未捕獲的型別錯誤:$(...)[1].offset 不是函式
我已經嘗試了很多東西,比如使用其他選擇器,使用 for 回圈,foreach ......我只能通過洗掉 [1] 從該陣列的第一個元素中獲取 offset().top 值,對于第二個,第三等...元素我似乎無法理解。
uj5u.com熱心網友回復:
因為$('.example p')[1]
回傳節點 HTML(元素 html)
如果您想使用 jquery 獲得偏移量,您可以使用:
$('.example p').eq(1).offset()
uj5u.com熱心網友回復:
似乎 jQuery 不回傳 jQuery 物件陣列。例如檢查是否$('.example p')[1]
是一個 jQuery 物件。您應該像這樣轉換 jQuery 物件中的所有專案:
var allExample = $('.example p').map((i, e) => $(e))
然后重試:
console.log(allExample[1].offset())
uj5u.com熱心網友回復:
使用簡單地遍歷選擇器中的每個".example p"
元素each
見演示:
$(".example p").each(function(i)
{
console.log( $(this).offset().top);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="example">
<p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers.
</p>
<p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers.
</p>
</div>
<div class="something">
<p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers.
</p>
<p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers.
</p>
</div>
<div class="example">
<p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers.
</p>
<p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers.
</p>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/486586.html
標籤:javascript jQuery 抵消
上一篇:未捕獲的ReferenceError:未定義“xxxxxx”
下一篇:嘗試用P替換Input