我可以使用什么代替:indeterminate jQuery选择器来避免IE 8中的异常?

时间:2013-01-31 12:23:37

标签: jquery internet-explorer-8

我有这样的代码

if ($('#chkCheckAll').is(':indeterminate') == true) 
{
}

但是它在ie 8

中抛出异常

在Jquery中可以做什么而不是使用ie8

3 个答案:

答案 0 :(得分:3)

请改用:

var $allChk = $('#chkCheckAll');
if ($allChk[0] && $allChk[0].indeterminate ) {
  ... 
}

问题是IE8 querySelectorAll实现不支持':indeterminate'伪表达式。但在你的情况下,它实际上不需要使用它,因为你可以查询DOM元素本身的相应属性

答案 1 :(得分:2)

这是一篇很棒的文章http://css-tricks.com/indeterminate-checkboxes/

IE8使用.prop进行排序,并且还有一个hack

<!-- Ghetto click handler -->
<input type="checkbox" id="cb1" onclick="ts(this)">function ts(cb) {
  if (cb.readOnly) cb.checked=cb.readOnly=false;
  else if (!cb.checked) cb.readOnly=cb.indeterminate=true;
}

答案 2 :(得分:0)

增强Ghetto处理程序以在表单提交中包含数据:

<!DOCTYPE html>
<html>
    <head>
        <style>
            .nodisplay {display: none;}
        </style>
        <script>
            function ts(cb) {
                if (cb.readOnly) 
                    cb.nextSibling.checked 
                    = cb.checked
                    = cb.readOnly
                    = false;
                else if (!cb.checked) 
                    cb.nextSibling.checked 
                    = cb.readOnly
                    = cb.indeterminate=true;
            }
        </script>
    </head>
    <body>
        <form>

            <input type="checkbox" id="cb1" name="cb1" onclick="ts(this)"
            ><input type="checkbox" name="cb1i" />

            <input type="submit">

        </form>
    </body>
</html>

结果是没有(未选中)cb1 = on(选中)或cb1i = on(indeterminate)。请注意,要使nextSibling在这种情况下工作,两个复选框之间必须没有空格,否则它会为您提供一个文本节点。