Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.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
jQuery根据类选择元素;但只选择最优秀的父母_Jquery - Fatal编程技术网

jQuery根据类选择元素;但只选择最优秀的父母

jQuery根据类选择元素;但只选择最优秀的父母,jquery,Jquery,我的HTML不是固定的,而是动态的。每次都会有不同的HTML,但我知道会出现所选的类ui 我想选择已选择类ui的元素,但只选择最顶端的父元素 就像下面的HTML一样,我应该有p标记和h3标记。我该怎么做 <p class="ui-selected"><span id='fracs' class="ui-selected">Air pollution is contamination of the indoor or outdoor environment by any c

我的HTML不是固定的,而是动态的。每次都会有不同的HTML,但我知道会出现所选的类ui

我想选择已选择类ui的元素,但只选择最顶端的父元素

就像下面的HTML一样,我应该有p标记和h3标记。我该怎么做

<p class="ui-selected"><span id='fracs' class="ui-selected">Air pollution is contamination of the indoor or outdoor environment by any chemical, <b class="ui-selected">physical or <span class="ui-selected"> biological </span>agent</b> <i class="ui-selected">that modifies the natural</i> characteristics of the atmosphere. Household combustion devices, motor vehicles, industrial facilities and forest fires are common sources of air pollution. Pollutants of major public health concern include particulate matter, carbon monoxide, ozone, nitrogen dioxide and sulfur dioxide. Outdoor and indoor air pollution cause respiratory and other diseases, which can be fatal.</span></p>
<h3 class="section_head1  ui-selected" >Key facts</h3>

谢谢你的帮助

使用body标记并选择direct children:

$("body > .ui-selected")
下面选择所有.ui选择的元素,然后选择它们,以仅提供顶级元素,即没有任何.ui选择的祖先

。。。我想这就是你想要的,至少

筛选器返回一个jQuery对象,以便您可以对其执行正常操作

$('.ui-selected').filter(function () {
    return !$(this).parents('.ui-selected').length;
}).on('click', function () {
    alert("Hi, I'm a top-most .ui-selected, and you just clicked me!");
});

我只想选择最上面的父元素,比如第一个p,然后是h3元素,而不是具有相同类的p标记的子元素。为什么$'p.ui-selected,h3.ui-selected'不起作用?我不需要所有的子元素,只需要最上面的子元素!这是最顶层的子级,至少在您的示例中是这样。这就是为什么我编写的html是非常动态的。@RGraham:我认为问题是选择了.ui的最顶层的子级可能不是最顶层的子级。@Matt这是有道理的。问题不太清楚。但我需要一个jquery对象作为执行下一个操作的回报。请?@developerCK:它将是一个jquery对象。当我在chrome控制台中运行给定的HTMLCreate解决方案时,它返回空数组,但为什么$'p.ui-selected,h3.ui-selected'不能工作?最近的include元素本身,应该是的,而不是父母
$('.ui-selected').filter(function () {
    return !$(this).parents('.ui-selected').length;
}).on('click', function () {
    alert("Hi, I'm a top-most .ui-selected, and you just clicked me!");
});