jquery:children()vs child selector“>”

前端之家收集整理的这篇文章主要介绍了jquery:children()vs child selector“>”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个表格,类似于以下内容
<tr>
    <td> <span class="myclass"></span>
    </td>
<tr>

我的$(this)被设置为tr元素,我正在尝试访问具有“myclass”类集合的Span元素。
以下似乎有效:

if ($(this).children('td').children('span').is('.myclass')){
    alert('in here');
}

但是当尝试使用它时:

if ($(this).children("td > span").is('.myclass')){

或这个:

if ($(this).children("td span").is('.myclass')){

它不是。我认为上述2中的任何一个都会得出类似的结果(尽管通过不同的方法),但显然不是。

我在这里缺少什么?

谢谢!

解决方法

孩子(选择器)只会匹配与选择器匹配的孩子。 tr的孩子(tds)都不能匹配td>跨度,因为tr没有跨度子元素,只有tds和td> span!== td

documentation也很清楚:

Get the children of each element in the set of matched elements,optionally filtered by a selector.

你可能想要的是.find()

$(this).find("td > span")

它返回与选择器匹配的所有后代,而不仅仅是孩子。

原文链接:https://www.f2er.com/jquery/181905.html

猜你在找的jQuery相关文章