这是一个bug,如何jQuery处理子选择器? [英] Is this a bug in how jQuery treats child selectors?

查看:149
本文介绍了这是一个bug,如何jQuery处理子选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个bug如何处理子选择器或者我错过了明显的东西?当孩子不是 * 时,我不能让它工作。

Is there a bug in how jQuery handles child selectors or am I missing out on something obvious? I can't get it to work when the child is anything other than *.

这里是jQuery选择器正在运行:

Here's the jQuery selector I am running:

$("#myTable > tr").each(function() {
    // do somthing }
);

表结构是:

<table id="myTable">
    <tr>
        <td><button>someButton</button></td>
        <td><textarea>...</textarea></td>
    </tr>
</table>

没有元素与上述选择器匹配 #myTable> tr 。但是下面列出的两个选择器工作正常。

No elements are matched with the above selector #myTable > tr. But the two selectors listed below work fine.

$("#myTable tr") // search all descendants for tr

或使用通配符匹配子项:

or use a wildcard to match children:

$("#myTable > *") // search all child elements


$ b b

对这里可能出现的错误有什么想法?

Any ideas on what could be wrong here?

感谢您的快速答案!

推荐答案

因为Firefox会自动添加 tbody 如果没有提供,则 tr 元素。你真的不能使用 table> tr

Its because Firefox automatically adds tbody elements around your tr elements if none are supplied. You really can't use table > tr.

您有:

<table id="myTable">
    <tr>
        <td><button>someButton</button></td>
        <td><textarea>...</textarea></td>
    </tr>
</table>

但是Firefox看到了这一点:

But Firefox sees this:

<table id="myTable">
  <tbody>
    <tr>
        <td><button>someButton</button></td>
        <td><textarea>...</textarea></td>
    </tr>
  </tbody>
</table>

其他元素将正常工作:

<div>
  <strong>Hi</strong>
</div>

和选择器:

alert( $("div > strong").text() ); // Alerts "Hi"

这篇关于这是一个bug,如何jQuery处理子选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆