2016-07-02 21 views
0
this     
<p>​adfad​</p>​       // value in 'this' 

this.toString() 
"[object HTMLParagraphElement]"  // of type HTMLParagraphElement 

this.outerHTML.indexOf('img')   // to find 'img' tag is there 
-1  

如何找到某個特定節點存在'HTMLParagraphElement'類型的Object? 除了上面提到的方法之外,還有更好的方法嗎?如何在「HTMLParagraphElement」類型的Object中找到某個特定節點?

+0

你能提供的例子,你說的html? – Mohammad

+0

adfadfadf

adfasdf


adfasdf

」 – ida

+0

請不要張貼在評論你的代碼,它是無法讀取到所有的實際目的(特別是在更長,更復雜,代碼的情況下)。 [編輯]你的問題,包括代碼,人們可以更容易地看到它。 –

回答

0

您可以檢查數組element.find(selector)的長度是否爲0,element.find(selector).length == 0其中選擇器可以是任何jquery選擇器。

console.log($('p').find('img').length); 
 
console.log($('p').find('br').length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<p>adfadfadf</p> 
 
<p>adfasdf</p> 
 
<p> 
 
    <br> 
 
</p> 
 
<p>adfasdf</p> 
 
<img />

相關問題