【发布时间】:2011-02-15 08:40:32
【问题描述】:
我有以下html代码:
<ul>
<li class="O">
<label for="radio1">Radio 1:</label>
<input type="radio" name="radio1" value="Yes" checked>Yes</input>
<input type="radio" name="radio1" value="No">No</input>
</li>
<li class="O">
<label for="checkbox1">Checkbox 1:</label>
<input type="checkbox" name="checkbox1" id="checkbox1" value="check"></input>
</li>
</ul>
使用 jQuery,我想对每个未选中的复选框或在 O 类的 li 元素内具有空值的元素做一些事情。这是我的方法:
$('li').each(function(){
if ($(this).hasClass('O')){
$(this).children(':not(label,input:radio)').each(function(){
if ( $(this).val() == "" || $(this).is(':checkbox:not(:checked)') ) {
//do something
}
});
}
此代码适用于 FF、Opera、Chrome 和 Safari,但不适用于 IE6-IE7。
对于两个 IE,使用 $(this).children(':not(label,input:radio)').length 获取 li 的长度返回 2 ,其他浏览器返回 0 和 1。
另外,获取第一个li的非标签子节点数($(this).children(':not(label)').length)返回4,同时获取子节点数($(this) .children().length) 返回 2。这对我来说毫无意义。
是我错了还是我使用的选择器中的某些内容与 IE 不完全兼容?
提前致谢。
【问题讨论】: