jQuery无法在IE 9.0.8中运行,但可以与开放的开发工具一起使用 [英] JQuery not working in IE 9.0.8, but works with dev tools open

查看:47
本文介绍了jQuery无法在IE 9.0.8中运行,但可以与开放的开发工具一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容适用于IE 9.0.8以外的所有浏览器.它会在div内使用ajax请求加载调查表.

The following works in all browsers except IE 9.0.8. It loads a survey form within div with an ajax request.

$('.tab-content').on('click', '.show_survey_form', function(e) {
  e.preventDefault()
  target = $(this).attr("data-target")
  my_href = $(this).attr("href")
  console.log("load: " + target + "   target: " + my_href)
  // load: #survey_response_form_33   target: /surveys/33/survey_responses/edit_multiple

  // Don't make a request unless the form is opening.
  if ($(this).hasClass('collapsed')) {
    console.log("Making request!")
    //$(target).load(my_href)
    $(this).html(closeSurveyForm) // Just changes the language on the button
  } else {
    $(this).html(respondToSurvey) // Just changes the language on the button
  }
}

.load在调试过程中被注释掉.在这种情况下,IE似乎在使用.hasClass时遇到问题.可以毫无问题地用在其他地方.

The .load is commented out during debugging. IE seems to have a problem using .hasClass in this context. It's used elsewhere with no issue.

真正奇怪的是,一旦我打开开发工具窗口,它就会开始工作.在此之前,它始终无效,并且在按F12键之后始终有效.

The really odd part is that the moment I open the dev tools window, it starts working. It consistently doesn't work before then, and consistently works after hitting F12.

其他问题表明,当类包含\ r字符时,hasClass方法不起作用,但此处并非如此.我正在使用jQuery 1.8.3.

Other issues have said the hasClass method doesn't work when the class contains a \r char but that isn't the case here. I'm using jQuery 1.8.3.

更新:将href更改为#"并将URL写入数据加载无效.在IE 9.0.8以外的所有浏览器中仍然可以使用.

Update: Changing the href to "#" and writing the URL into data-load had no effect. Still works in all browsers except IE 9.0.8.

推荐答案

这与jQuery或hasClass()无关.这完全取决于您对console.log()的使用.

This has nothing to do with jQuery or hasClass(). It is entirely down to your use of console.log().

重要的是要知道IE在打开F12开发工具窗口之前不会定义console对象.

It is important to know that IE does not define the console object until the F12 dev tools window has been opened.

这意味着在打开它之前,console调用将引发javascript对象未定义"错误.这样一来,似乎周围的代码将无法正常工作,而实际上只是缺少控制台对象.

This means that before you open it, the console calls will be throwing javascript "object undefined" errors. This will make it appear that the code around it isn't working, but it's really just the fact that the console object is missing.

在其他较旧的浏览器中,您可能会产生类似的效果,但是大多数当前浏览器版本均不执行此操作-它们会立即定义console对象,而不管开发工具是否打开. IE是唯一的例外.

You may have similar effects in other older browsers, but most current browser versions do not do this -- they define the console object immediately, regardless of whether the dev tools is open or not. IE is the only exception.

您可以通过以下方法解决此问题:(a)不使用console,除非您实际上正在调试并打开开发工具,或者(b)在所有console调用中添加if(console)检查.这样可以避免错误.

You can get around this by either (a) not using console unless you are actually debugging and have the dev tools open, or (b) adding an if(console) check to all your console calls. This will prevent the error.

此处的更多信息:为什么JavaScript仅在IE中打开开发人员工具一次后才能工作?

这篇关于jQuery无法在IE 9.0.8中运行,但可以与开放的开发工具一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆