如何选择下一个匹配元素? [英] How to select next matching element?

查看:104
本文介绍了如何选择下一个匹配元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

<a href="#/link1">link1</a>
<a href="#/link2">link2</a>
<a href="#/link3">link3</a>

$("a[href*='#/link1'").next('a[href*="#"]').click();

但这不是:

<a href="#/link1">link1</a><br>
<a href="#/link2">link2</a><br>
<a href="#/link3">link3</a><br>

$("a[href*='#/link1'").next('a[href*="#"]').click();

以上测试< br>标签,而不是下一个匹配链接。

The above tests the "< br >" tag, not the next matching link.

我将如何在两种情况下使其工作。我想选择下一个匹配元素,如果匹配则不是下一个元素。也许.next()不是正确的方法?

How would I go about making it work in both situations. I want to select the next matching element, not the next element if it matches. Maybe .next() isn't the correct method?

推荐答案

第二个例子不应该选择任何东西。阅读 next 的文档:

The second example should not select anything at all. Read the documentation of next:


在匹配元素集合中获取每个元素的紧随其后的兄弟。如果提供了一个选择器,只有当它与那个选择器匹配时,它才会检索下一个兄弟。

Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.

你必须使用 nextAll 获取所有兄弟姐妹并相应地过滤它们:

You have to use nextAll to get all siblings and filter them accordingly:

$("a[href*='#/link1']").nextAll('a[href*="#"]:first').click();

参考 nextAll

这篇关于如何选择下一个匹配元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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