我想知道一個div的第一個孩子是否是iframe,如果是的話,請把它加到另一個div上。我正在做一些與圖像非常相似的東西,但它們更容易定位,因爲它們始終位於第一個元素的內部。這就是我所拋出的children[0] is undefined
錯誤。如果第一個孩子是iframe
$(".post").each(function() {
if($(this).find(".post-excerpt").children[0].nodeName.has('iframe').length){
$(this).prepend("<div class='featured-video'></div>");
$(this).find(".post-excerpt").children[0].nodeName.has('iframe').prependTo($(this).find(".featured-video"));
}
});
我的HTML看起來像這樣:
<article class="post twelve columns centered">
<header class="post-header">
<h2 class="post-title"><a href="#">Title</a></h2>
</header>
<section class="post-excerpt">
<iframe height="166" src="http://something.com"></iframe>
</section>
</article>
其實,這裏的第一個答案完美,但它被刪除。 if($(this).find(「。post-excerpt:first-child」)。('iframe'))''。不知道爲什麼它被刪除,但它工作得很好。我相信我在想這件事。 –