如何获得下一个图像标签后特定的dom标签,兄弟姐妹呢? [英] how to get the next image tag after specific dom tag, sibling or not?

查看:88
本文介绍了如何获得下一个图像标签后特定的dom标签,兄弟姐妹呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们假设

<html>
<h1>heading</h1>
<div>
  <div>
      <span>
            <img src="source"/>
       </span>
  </div>
</div>
 </html>

上面只是一个例子,它应该适用于任何html代码。


我需要< h1> 之后的下一个图片标签,无论 img 放置于
$('h1')。nextAll('img')。first() $('h1')。nextUntil('img')。last()。next()不会在这里工作,因为它们不是< h1& 。有没有办法我可以得到 img 没有写太多的循环代码?

The above is just an example, it should work for any html code.

I need the next image tag after <h1> no matter where the img is placed
$('h1').nextAll('img').first() or $('h1').nextUntil('img').last().next() won't work here because they are not siblings of <h1>. Is there a way I can get the img without writing too much loop code?

推荐答案

var $h1 = $("h1"); // Your `h1`
var all = $("*"); // All DOM elements

var h1idx = all.index($h1);
all.splice(0, h1idx); // Get rid of everything before and until your `h1`

var result = all.filter("img").first(); // Your first `img`

这篇关于如何获得下一个图像标签后特定的dom标签,兄弟姐妹呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆