Internet Explorer中的jquery show()/ hide()问题? [英] jquery show()/hide() problem in Internet Explorer?
问题描述
以下html代码适用于Firefox,但由于某种原因在IE中失败(Label2未显示)。这是一个错误还是我错过了什么?
The following html code works in Firefox, but for some reason fails in IE (Label2 is not shown). Is that a bug or I miss something?
任何帮助都将不胜感激。
Any help would be appreciated.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript">
</script>
<script>
jQuery(document).ready(function(){
function setVis(){
var val = $("#check").is(":checked");
if (val)
$("#g_1").show();
else
$("#g_1").hide();
}
setVis();
$("#check").change(setVis);
});
</script>
</head>
<body>
<span>
<input type="checkbox" id="check" />
<label>Label1</label>
</span>
<span id="g_1">
<label>Label2</label>
</span>
</body>
</html>
推荐答案
原因:
每个MSDN,更改活动是
...当内容提交
时触发,而不是在值更改时触发。
例如,在文本框中,当用户键入时,
不会触发此事件
,而是当用户通过保留$ $的文本框提交
更改时b $ b有焦点。
...fired when the contents are committed and not while the value is changing. For example, on a text box, this event is not fired while the user is typing, but rather when the user commits the change by leaving the text box that has focus.
复选框的行为类似:在IE中,直到焦点从元素中移除才会触发事件(尝试一下:单击然后选中复选框,行为符合预期)。 Firefox显然不尊重这种区别。
Behavior for checkboxes is similar: in IE the event does not fire until focus is removed from the element (try it: click then tab off of the checkbox and behavior is as expected). Firefox apparently does not respect this distinction.
使用点击
事件处理程序而不是更改
:
Use the click
event handler instead of change
:
$("#check").click(setVis);
...并且浏览器的行为应该保持一致。
...and behavior should be consistent across browsers.
这篇关于Internet Explorer中的jquery show()/ hide()问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!