[英]Comparing DOM elements with jQuery
I am working inside of a jQuery each
iterator: 我在jQuery
each
迭代器内部进行工作:
$('p').each(function(){ ... });
I'd like to create an expression that evaluates to true when: 我想创建一个表达式,在以下情况下计算结果为true:
$(this)
is the last p
element $(this)
是最后一个p
元素 $(this).parent()
$(this).parent()
$(this)
must be a direct child of $(this).parent()
$(this)
必须是$(this).parent()
的直接子代 $(this)
is not necessarily the last direct child of $(this).parent()
$(this)
不一定是$(this).parent()
的最后一个直接子$(this).parent()
Here are a few scenarios, with the desired p
marked by asterisks: 以下是一些情况,所需的
p
用星号标记:
<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() { /* ... */ });
});
which would return all your wanted <p>
tags. 它将返回所有想要的
<p>
标签。
Here is a demo: http://jsbin.com/orage | 这是一个演示: http : //jsbin.com/orage | See Source
见资料
Your original requirements does not make any sense. 您的原始要求没有任何意义。
$(this)
must be a direct child of$(this).parent()
$(this)
必须是$(this).parent()
的直接子代
$(this)
is ALWAYS a direct child of $(this).parent()
by definition of what parent()
does. $(this)
始终是一个直接子$(this).parent()
由什么的定义parent()
一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.