【问题标题】:jQuery last-child throws exception in IE8jQuery last-child 在 IE8 中抛出异常
【发布时间】:2012-08-14 12:20:02
【问题描述】:

我有一个包含 3 列的表格。第三列仅包含用于对行进行排序的标签。在 $(document).ready 上,我使用 jQuery 隐藏第三列(仅包含标签)并使用

向其他列添加样式类
$(document).ready(function() {
$("#songListTable tr td:nth-child(3)").hide().width(0); 
$("#songListTable tr td:nth-child(1)").addClass("songTitle");
$("#songListTable tr td:nth-child(2)").addClass("songPerformer");
});

这适用于所有浏览器。我添加了 .width(0) 以确保隐藏列不占用布局中的任何空间。

然后我使用表单选择发送一个值:

<form id="songSortForm" name="songSortForm" method="post" action="">
<select name="songSortSelect" id="songSortSelect" onChange=sortSongs(this.value)>

到一个脚本循环遍历列中的所有行,检查该行中的第三个 td 是否包含该值,并根据结果显示或隐藏整行。

function sortSongs (tagQuery) {
$("#songListTable tr").each(function() {
    $thisText= $(this).find("td:last-Child").html();
    $(this).show();

    if ($thisText.indexOf(tagQuery)>-1) {
    $(this).show();
    }
else {
    $(this).hide();
    } 
})
};

这在除 IE 之外的所有浏览器中都能很好地工作。 IE 开发人员工具显示抛出异常,但未在 jQuery1.6.2 脚本中捕获(第 17 行,字符 12370)。但是,当我逐步执行脚本时,每次我在排序脚本中使用“td:last-Child”时似乎都会抛出错误。脚本中断。 last-Child 在 (document).ready 脚本中没有抛出错误,所以这可能是我自己的代码中的错误,但是我一直在寻找 2 天在网上找到答案。

附加到表格中 anthying 的唯一样式是 font-family、color、width、margin 和 text-align。

任何帮助将不胜感激!

您可以在以下位置查看测试页面:http://donbryn.ipage.com/media.php (点击“歌曲列表”)。

【问题讨论】:

  • 我仍然不知道为什么 last-Child 在 IE8 中会抛出异常,但是我将其更改为 nth-child(3) 并且它可以工作。它只对我有用,因为我知道这个表中总是有 3 列,所以它解决了我的直接问题,但如果有人知道为什么 last-Child 在 IE 中不起作用,我仍然想知道一个工作 -大约。在我所有的搜索中,jQuery last-Child 是使 CSS3 选择器在 IE 中工作的解决方法。
  • 我没有启动我的虚拟机,所以现在无法正确测试,但是您尝试过正确(小写)版本的伪类吗? :last-child
  • 仅供参考,我今天遇到了类似的问题,但在我的情况下,异常是由 :nth-last-child(2) 引发的,而 not 是由 :last-孩子。 (我可以使用 :last-child 和更多代码来替换 :nth-last-child(2)。)问题出现在 IE8 和 jQuery 1.7.1 上。

标签: jquery internet-explorer-8 css-selectors


【解决方案1】:

我认为那是因为 IE8 不支持:last-child 选择器,而只支持:first-child

【讨论】:

    猜你喜欢
    • 2013-09-26
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    相关资源
    最近更新 更多