5

以下适用于除 IE 9.0.8 以外的所有浏览器。它使用 ajax 请求在 div 中加载调查表。

$('.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 似乎有问题。它在其他地方使用没有问题。

真正奇怪的是,当我打开开发工具窗口时,它就开始工作了。在此之前它始终不起作用,并且在达到 F12 后始终起作用。

其他问题表明当类包含 \r 字符时 hasClass 方法不起作用,但这里不是这种情况。我正在使用 jQuery 1.8.3。

更新:将 href 更改为“#”并将 URL 写入数据加载无效。除 IE 9.0.8 外,仍然适用于所有浏览器。

4

1 回答 1

12

这与 jQuery 或hasClass(). 这完全取决于您对console.log().

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

这意味着在您打开它之前,console调用将引发 javascript“对象未定义”错误。这将使它看起来周围的代码不起作用,但这实际上只是缺少控制台对象的事实。

您可能在其他较旧的浏览器中也有类似的效果,但大多数当前浏览器版本都不会这样做——它们会console立即定义对象,而不管开发工具是否打开。IE 是唯一的例外。

您可以通过 (a) 不使用来解决此问题,console除非您实际上正在调试并打开开发工具,或者 (b)if(console)为所有console调用添加检查。这将防止错误。

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

于 2013-05-21T20:16:49.393 回答