检查元素是否在 jQuery 中的另一个元素之前或之后
假设我有这个标记
<h3 id="first">First</h3>
<p>Hi</p>
<p>Hello</p>
<h3 id="second">Second</h3>
<p>Bye</p>
<p>Goodbye</p>
,如何以编程方式检查某个元素(例如其中一个 p
)是否位于第一个 h3
之后和第二个 h3 之前
,用 jQuery?
我正在尝试做这样的事情:
$(p).each(function(){
if($(this).isAfter("#first") && $(this).isBefore("#second")) {
// do stuff
}
});
Let us suppose I have this markup
<h3 id="first">First</h3>
<p>Hi</p>
<p>Hello</p>
<h3 id="second">Second</h3>
<p>Bye</p>
<p>Goodbye</p>
How can I programmatically check if an element, such as one of the p
s, is after first h3
and before the second h3
, with jQuery?
I am trying to do something like this:
$(p).each(function(){
if($(this).isAfter("#first") && $(this).isBefore("#second")) {
// do stuff
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果 sel 是一个真正的选择器,Rocket 的解决方案可以正常工作,但如果您传递 jquery 对象,它就不起作用。
如果您想要一个同时适用于选择器和 jquery 对象的插件,只需使用我稍微修改过的版本即可:
感谢 Rocket 的原始解决方案。
Rocket's solution works fine if
sel
is a real selector, if you pass a jquery object though, it won't work.If you want a plugin that works with both selectors and jquery objects, just use my slightly modified version instead:
Kudos to Rocket for the original solution.
要查看一个元素是否在另一个元素之后,可以使用
prev()
或prevAll()
获取前一个元素,然后查看如果匹配的话。要查看一个元素是否在另一个元素之前,您可以使用
next()
或nextAll()
获取下一个元素,并且看看是否匹配。演示:http://jsfiddle.net/bk4k7/
To see if an element is after another, you can use
prev()
orprevAll()
to get the previous element(s), and see if it matches.And to see if an element is before another, you can use
next()
ornextAll()
to get the next element(s), and see if it matches.DEMO: http://jsfiddle.net/bk4k7/
您可以使用
index()
函数:You can use the
index()
function:这是一个 小提琴
Here is a fiddle
我实现了一个通用的 isAfter 函数,它考虑了深度和 DOM 树,并且可以正确确定
a
是否在b
之后,即使它们在 2 中不同的子树:您可以在 我的 GitHub 存储库 中找到它。感谢您对代码的反馈
I implemented a general
isAfter
function, which considers the depth and DOM tree and can correctly determine ifa
is afterb
even if they are in 2 different subtrees:you can find it in my GitHub Repository. Would appreciate your feedback on the code
答案:
无论选择器 (sel) 如何,都会选择所有先前的元素或下一个元素,因此我进行了以下修改,根据类选择元素:
The answer:
Was selecting all previous elements or next elements regardless of the selector (sel) so I made the following modifications that select elements based on class:
此版本适用于任何深度,比较两个元素是否在彼此之前、相等或之后。分别返回 -1、0 和 1。
它的工作原理是获取传递给比较函数的元素的所有索引偏移量。
然后它比较两个偏移量数组,并在发现差异时停止迭代。
This version works at any depth comparing if two elements are before, equal, or after each other. Returning -1, 0, and 1 respectively.
How it works is that it gets all the index offsets of the elements passed into the compare function.
Then it compares the two array of offsets and stops iterating when it finds a discrepancy.
此单行检查适用
于本示例中使用的问题中的每种情况 ID:
querySelectorAll
方法中的 ID 顺序不相关,只需要使用在这种情况下选择两个元素的选择器#第二,#第一
。如果您需要多次使用此功能,则可以使用此函数:
即使在特殊情况下,此函数也不会出现错误:
false
true
false
This one-liner check will work in every case
IDs from your question used for this example:
Order of IDs in
querySelectorAll
method is not relevant it is only necessary to use selector that will select both elements in this case#second, #first
.If you need to use this multiple times you can use this function:
This function will work without errors even in exceptional cases:
false
true
false
记住 Rocket 的答案,我更喜欢使用
.index()
方法,如下所示:它似乎更适合阅读/理解,并且在行选择中完美工作。
Keeping in mind Rocket's answer I prefer to use
.index()
approach like following:It seems more clear for reading/understanding and works perfectly in rows selection.
如果所讨论的元素是兄弟元素(它们在原始问题中),则前面的答案可以正常工作,但更通用的解决方案将考虑以下场景:
更通用的解决方案是:
其中“上下文”是一个可选参数,仅限制jQuery 的搜索区域可提高性能。我没有执行任何测试,但“上下文”可能是不必要的,因为 $("*") 似乎执行速度很快。另请注意,如果任一元素不在上下文中,则 index() 将返回 -1。
当然,这一切都假设“之前”和“之后”是指它们在文档模型中的顺序。如果您担心它们在显示上的相对位置,您可能需要检查元素相对于文档的显示坐标,因为“相对”、“固定”和“绝对”CSS 位置使一切成为可能。
The previous answers work fine IF the elements in question are siblings (which they are in the original question) but a more general solution would consider the following scenario:
A more general solution would be:
Where "context" is an optional parameter that merely limits the search area for jQuery to improve performance. I haven't performed any tests but "context" is probably unnecessary since $("*") seems to execute qualitatively fast. Also note that index() will return -1 is either element is not within the context.
Of course this all assumes that by "before" and "after" you mean their order in the document model. If you are concerned about their relative positions on the display you would want to examine the elements display coordinates relative to the document as "relative", "fixed" and "absolute" CSS positions make anything possible.
仅当元素处于同一级别时,接受的答案才有效。如果您想要定位第一个元素(无论级别如何),解决此问题的一种方法是:
The accepted answer only works if the elements are at the same level. One approach to solve it if you want to locate which element is first regardless of the level is: