if ($("#makespan").is(":visible") == true) {
var make = $("#make").val();
}
else {
var make = $("#othermake").val();
}
Make:<span id=makespan><select id=make></select><span id=othermakebutton class=txtbutton>Other?</span></span><span id=othermakespan style="display: none;"><input type=text name=othermake id=othermake> - <span id=othermakecancel class=txtbutton>Cancel</span></span>
上面的代码在Firefox中运行流畅,但在Chrome中似乎行不通。在Chrome中,它显示.is(":visible") = false,即使它是真的。
我使用以下jQuery版本:jQuery -1.4.3.min.js
jsfiddle.net/wju2r/4/
如果一个项是隐藏项的子项,is(":visible")将返回true,这是不正确的。
我只是通过添加“display:inherit”到子项来修复这个问题。这将为我解决这个问题:
<div class="parent">
<div class="child">
</div>
<div>
和CSS:
.parent{
display: hidden;
}
.child{
display: inherit;
}
现在可以通过改变父元素的可见性来有效地打开和关闭项目,$(element).is(":visible")将返回父元素的可见性
有一种奇怪的情况,如果元素被设置为显示:内联jQuery检查可见性失败。
例子:
CSS
#myspan {display: inline;}
jQuery
$('#myspan').show(); // Our element is `inline` instead of `block`
$('#myspan').is(":visible"); // This is false
要解决这个问题,你可以在jQuery中隐藏元素,然后show/hide或toggle()应该可以正常工作。
$('#myspan').hide()
$('#otherElement').on('click', function() {
$('#myspan').toggle();
});