【问题标题】:Dynamically loading jQuery mobile causes IE to minimize动态加载 jQuery mobile 导致 IE 最小化
【发布时间】:2011-08-23 15:34:42
【问题描述】:

好的,这是迄今为止我遇到的最奇怪的错误。不过这很简单。如果我在任何版本的 Internet Explorer 中动态加载 jQuery 和 jQuery mobile,实际的 IE 窗口最小化。通过 IETester 的所有 IE 版本都会发生这种情况,但是,如果我在 IE9 中运行完整版,它会启动兼容模式并且由于某种原因不会最小化。

我尝试了各种加载脚本的方法(在示例代码中进行了注释),结果都是相同的。

为什么会这样?有办法解决吗?

http://jsfiddle.net/Xeon06/RCsuH/

【问题讨论】:

  • 我可以确认我在普通 VM 上的 IE 7 中看到了这种情况。具体来说,窗口没有最小化 - 它被发送到堆栈的底部(最小化其他窗口,你会发现你的 IE 窗口仍然打开),感觉就像在某个地方调用了 window.blur() . (在我的 IE 9 上,它既不会最小化/模糊,也不会进入兼容模式。)

标签: javascript jquery debugging internet-explorer jquery-mobile


【解决方案1】:

这是 jQuery mobile 中的 known issue。违规行是jquery.mobile.navigation.js:913

// Kill the keyboard.
// XXX_jblas: We need to stop crawling the entire document to kill focus. Instead,
//            we should be tracking focus with a live() handler so we already have
//            the element in hand at this point.
// Wrap this in a try/catch block since IE9 throw "Unspecified error" if document.activeElement
// is undefined when we are in an IFrame.
try {
    $( document.activeElement || "" ).add( "input:focus, textarea:focus, select:focus" ).blur();
} catch(e) {}

blur() 的调用将 IE 窗口发送到堆栈的后面。

作为一种解决方法,您可以通过将脚本标签以物理方式 放置在 HTML 的 <head> 中来避免这种情况。

<!DOCTYPE HTML>
<html>
  <head>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/latest/jquery.mobile.css" />
    <script src="http://code.jquery.com/jquery-1.6.2.js"></script>
    <script src="http://code.jquery.com/mobile/latest/jquery.mobile.js"></script>

...

将脚本标签放在文档的其他位置或通过脚本插入它们会触发错误。

This Fiddle 演示了实际的解决方法。请注意,这只适用于顶级文档。如果文档在&lt;iframe&gt; 中,仍然会出现该错误。因此,如果您在 IE 7/8 中打开 JSFiddle 编辑器,它仍然会被发送到后面;但如果你只打开rendered HTML,它就不会。

【讨论】:

    【解决方案2】:

    我尝试“修复”它:http://jsfiddle.net/RCsuH/6/

    @josh3736 几乎完全正确,它在代码的某处触发了document.body.blur(),这导致了窗口的最小化。

    我的修复只是用无操作函数替换了该函数。我无法让脚本标签在完成加载后触发 onload,因此替换函数(如有必要)由您决定。

    然而,所有这些似乎都是 jQuery Mobile 库中的错误,因此您可能应该向他们提交错误报告。但是,我不确定是否会因为 IE 上存在针对手机/平板电脑的框架的错误而给他们带来太多困扰。

    注意:这是替换本机函数的可怕、可怕的代码。如果可能,请不要使用它。

    【讨论】:

    • “我不确定是否会因为 IE 上针对手机/平板电脑的框架存在错误而给他们带来太多困扰。” IE 在手机/​​平板电脑上,随着新的 Windows 8 平板电脑的出现,这将是 IE9/10。我的网站允许用户创建移动页面,而 preview 使用他们移动页面的实际页面 HTML/CSS/JS(这是一个好主意,原因很明显,对)。它在 IE9 中中断。
    猜你喜欢
    • 1970-01-01
    • 2013-01-23
    • 2012-05-20
    • 2012-12-12
    • 2013-01-18
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多