Javascript jQuery选择器';:非();或方法';。非();IE9中的行为

Javascript jQuery选择器';:非();或方法';。非();IE9中的行为,javascript,jquery,internet-explorer-9,Javascript,Jquery,Internet Explorer 9,以下代码段已使用chrome 23、firefox 3.5和IE9进行了测试: <!DOCTYPE html> <html><head> <title>test</title> <script src="jquery-1.7.2.min.js"></script> <script> $(function() { $('

以下代码段已使用chrome 23、firefox 3.5和IE9进行了测试:

<!DOCTYPE html>
<html><head>
      <title>test</title>
      <script src="jquery-1.7.2.min.js"></script>
      <script>
          $(function() {
              $('#id1 *').not('.c1 *').attr('disabled', true);
          });
      </script>
</head><body>
      <div id="id1">
            <div class="c1">
                <input type=radio>td1</input>
            </div>
            <div class="c2">
                <input type=radio>td2</input>
            </div>
      </div>
</body></html>

测试
$(函数(){
$(“#id1*”).not(“.c1*”).attr('disabled',true);
});
td1
td2
仅应禁用td2,但通常的可疑(IE9)禁用td1和td2。
你将如何解决这个问题

回答
实际上,问题不在于jquery选择器,而在于当元素在IE9中获得属性“disabled”时,所有子元素都被禁用。但是,这在Chrome和FF中不会发生(至少在上述版本中是如此)。

此处的详细信息:

请改用此选择器:

$('#id1 :not(.c1) input').attr('disabled', true);


您所写的内容可能应该这样重新编写:

$('#id1 *').not('.c1').find("*").attr('disabled', true);
$('#id1 div:not(.c1) :radio').prop('disabled', true);


IE可能正在做正确的事情:

  • $(“#id1*”)
    选择
    #id1
    中的所有标记,这将为您提供两个div和两个输入标记
  • .not(“.c1*”)
    从上面的列表中删除
    .c1
    中的输入标记,您将得到三个元素:两个div和一个input
  • 对结果应用
    disabled=disabled
    。在IE中,
    禁用div标记及其内部的所有内容
我相信你可以达到这样的预期效果:

$('#id1 *').not('.c1').find("*").attr('disabled', true);
$('#id1 div:not(.c1) :radio').prop('disabled', true);

您是否尝试过使用
input
而不是
*
?您的筛选不正确,这不是IE9的问题。pimvdb:感谢您的评论:原始选择器
$(“#id1*”)。不是(“.c1*”)
实际上在使用jquery 1.8.3的所有3个浏览器中都能工作,但是Hunter的版本
$(“#id1:not(.c1)*'))
更准确,在jquery 1.7.2的所有3种浏览器中都可以使用。您能告诉我它有什么区别吗?它检查
c1
是否是唯一的类,并设置和重置。另外,最好使用一个否定的
if
。在你的
中,你可以写
$(这个)。找到(“*”).attr('disabled',!$(这个)。hasClass('c1'))
好的,我明白你的意思了。谢谢@hunter,我忘记了
hasClass
方法。几乎,确保在禁用时选择的是
.c1
div
中的
输入,而不是
.c1
div
本身。生成的HTML:
td1 td2