Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将DOM元素与jQuery进行比较_Jquery - Fatal编程技术网

将DOM元素与jQuery进行比较

将DOM元素与jQuery进行比较,jquery,Jquery,我在jQuery每个迭代器中工作: $('p')。每个(函数(){…}) 我想创建一个表达式,该表达式在以下情况下计算为true: $(this)是最后一个p元素 作用域是$(this).parent() $(this)必须是$(this.parent() $(this)不一定是$(this.parent() 以下是一些场景,所需的p用星号标记: <div> <p>div1 p1</p> <p>div1 p2</p> &

我在jQuery
每个迭代器中工作:

$('p')。每个(函数(){…})

我想创建一个表达式,该表达式在以下情况下计算为true:

  • $(this)
    是最后一个
    p
    元素
  • 作用域是
    $(this).parent()
  • $(this)
    必须是
    $(this.parent()
  • $(this)
    不一定是
    $(this.parent()
以下是一些场景,所需的
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>

第1部分p1

第1部分p2

第一组p3***

第2分部p1***

第2分部s1 第3分部p1***

第3分部d1 第3分部p2


我会发布我的解决方案尝试,但失败的太多了。谢谢您的帮助。

您可以使用如下选择器:

$('body > div').each(function() {
    $(this).children('p:last').each(function() { /* ... */ });
});
这将返回您想要的所有标签

这里有一个演示:|


你最初的要求毫无意义

$(this)
必须是
$(this.parent()

$(this)
始终是
$(this.parent()
的直接子级,根据
parent()
的定义