比较DOM元素和jQuery [英] Comparing DOM elements with jQuery
问题描述
我正在使用jQuery each
迭代器:
I am working inside of a jQuery each
iterator:
$('p').each(function(){ ... });
我想创建一个表达式,该表达式在以下情况下计算为true:
I'd like to create an expression that evaluates to true when:
-
$(this)
是最后一个p
元素 - 范围是
$(this).parent()
-
$(this)
必须是$(this).parent()
的直接子代
-
$(this)
不一定是$(this).parent()
的最后一个直接子级
$(this)
is the lastp
element- scope is
$(this).parent()
$(this)
must be a direct child of$(this).parent()
$(this)
is not necessarily the last direct child of$(this).parent()
以下是几种情况,所需的p
用星号标记:
Here are a few scenarios, with the desired p
marked by asterisks:
<div>
<p>div1 p1</p>
<p>div1 p2</p>
<p>div1 p3***</p>
</div>
<div>
<p>div2 p1***</p>
<span>div2 s1</span>
</div>
<div>
<p>div3 p1***</p>
<div>
div3 d1
<p>div3 p2</p>
</div>
</div>
我会发布解决方案的尝试,但是失败的案例太多了.感谢您的帮助.
I'd post my attempts at a solution, but there have been too many failed ones. Thanks for the help.
推荐答案
您可以使用如下选择器:
You could use a selector such as this:
$('body > div').each(function() {
$(this).children('p:last').each(function() { /* ... */ });
});
这将返回您想要的所有<p>
标签.
which would return all your wanted <p>
tags.
这里是一个演示: http://jsbin.com/orage | 参见源文件
Here is a demo: http://jsbin.com/orage | See Source
您的原始要求没有任何意义.
Your original requirements does not make any sense.
$(this)
必须是$(this).parent()
根据parent()
的定义,
$(this)
始终是$(this).parent()
的直接子代.
$(this)
is ALWAYS a direct child of $(this).parent()
by definition of what parent()
does.
这篇关于比较DOM元素和jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!