jQuery 圆角 + IE8 中的 focus()
我正在 ASP.NET MVC 中开发 Web 应用程序。我最近使用 jQuery Rounded Corners 插件添加了圆角 (http://plugins.jquery.com/project /角)。这似乎让 IE8 感到不安(但不是 6 或 7,或 Firefox),因为我无法再在 $(document).ready() 中设置焦点。
这是问题的一个示例:
$(document).ready(function() {
// using jQuery rounded corners plugin
$("#centre").corners();
// this doesn't have any effect in IE8
$("#emailAddress").focus();
});
其中#centre 包含我们的登录页面,#emailAddress 是其中的第一个字段。
emailAddress 字段应该获得输入焦点。在 IE6 和 IE6 中确实如此。 7 和 Firefox,但 IE8 不支持。如果你注释掉两条圆角线,那么它就可以工作。
我还尝试将焦点线移动到 $(window).load():
$(window).load(function() {
$("#emailAddress").focus();
});
这也不起作用。但是,如果我写使用超时设置焦点,那么它会起作用:
$(document).ready(function() {
// using jQuery rounded corners plugin
$("#centre").corners();
// this doesn't have any effect in IE8
setTimeout(function() { $("#emailAddress").focus(); }, 100);
});
我不确定为什么在超时后设置焦点应该起作用。是否是因为圆角操作修改了 DOM(它们在容器的顶部和底部添加了一系列 DIV 来创建圆角边框),这需要一些时间,并且 focus() 是在 DOM 完成之前发生的更新?
感谢您的帮助
艾德
I am developing a web application in ASP.NET MVC. I have recently added rounded corners using the jQuery Rounded Corners plugin (http://plugins.jquery.com/project/corners). This seems to have upset IE8 (but not 6 or 7, or Firefox) because I can no longer set the focus in $(document).ready().
Here is an example of the problem:
$(document).ready(function() {
// using jQuery rounded corners plugin
$("#centre").corners();
// this doesn't have any effect in IE8
$("#emailAddress").focus();
});
where #centre contains our login page, and #emailAddress is the first field within it.
The emailAddress field should get the input focus. It does in IE6 & 7 and Firefox, but not in IE8. If you comment out the two rounded corners lines, then it works.
I have also tried moving the focus line to $(window).load():
$(window).load(function() {
$("#emailAddress").focus();
});
This doesn't work either. However if I write set the focus using a timeout, then it works:
$(document).ready(function() {
// using jQuery rounded corners plugin
$("#centre").corners();
// this doesn't have any effect in IE8
setTimeout(function() { $("#emailAddress").focus(); }, 100);
});
I am not sure why setting focus after a timeout should work. Could it be because the rounded corner operations modify the DOM (they add a series of DIVs at the top and bottom of the container to create the rounded border), which takes some time, and the focus() is happening before the DOM has finished updating?
Thanks for your help
Ed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
既然您正在执行超时并且它有效,那么表单元素是否会在文档准备好后加载? IE是用ajax加载的还是用javascript创建的?
我知道 JQuerycorns 尝试在每个浏览器中使用 CSS3,但如果是 IE,它会生成一堆 div。难道只是在IE6/7中需要更长的时间,所以不需要超时> >
Since you're doing a timeout and it works, could it be that the form element is loaded after the document is ready? I.E. loaded with ajax or created with javascript?
I know that JQuery corners tries to use CSS3 per browser, but if it's IE it makes a bunch of divs. Could it be that it just takes longer in IE6/7 so the timeout is unneeded>