簡體   English   中英

使用jQuery檢查某個元素是否是另一個元素中的第一件事

[英]Check, with jQuery, if an element is the first thing in another element

有沒有一種方法可以檢查child div是否是parent div的第一項?

例如:

<div id="parent">
   Some text                   <--------- not the first thing in the div
   <div id="child"></div>
</div>

要么

<div id="parent">
   <div id="child"></div>      <--------- is the first thing in the div
   Some text
</div>

就像是

var isFirst = $('#child').is($('#parent').contents().filter(function () {
    return this.nodeType != 3 || $.trim(this.nodeValue) != ''
}).first())

演示: 第一不是

需要檢查的條件很少

  1. 需要測試文本節點,因此必須使用.contents()
  2. 由於必須將空文本節點留空,因此需要使用過濾器將空文本節點過濾掉

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM