jQuery 的 .load() 在 IE 中不起作用 - 但在 Firefox、Chrome 和 Safari 中很好

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1061525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:32:04  来源:igfitidea点击:

jQuery's .load() not working in IE - but fine in Firefox, Chrome and Safari

internet-explorerjquery

提问by S.Kiers

I'm hitting my head on the wall against this one ...

我的头撞在墙上,撞到了这个……

I have the following code:

我有以下代码:

$("#home").click(function(e) {
    $(".tabs").attr("src","tabs-home.gif");
    $(".islice").hide('fast');
    $(".islice").load("home.html");
    $(".islice").show('fast');  
    e.preventDefault();
});

It works perfectly fine in Firefox, Safari and Chrome, but IE only runs attr() and does not do either the hide/show or the load. I tried removing the hide and show and it still does not work.

它在 Firefox、Safari 和 Chrome 中运行良好,但 IE 只运行 attr() 并且不执行隐藏/显示或加载。我尝试删除隐藏和显示,但它仍然不起作用。

IE reports no syntax errors, even with DebugBar. What could I be doing wrong?

即使使用 DebugBar,IE 也不会报告任何语法错误。我可能做错了什么?

You can see the live site at http://www.brick-n-mortar.com

您可以在http://www.brick-n-mortar.com 上查看实时站点

回答by S.Kiers

I am having the same problem. Many sites I have found have suggested that IE may be caching your code and suggest to append the code to

我有同样的问题。我发现的许多站点都表明 IE 可能正在缓存您的代码并建议将代码附加到

$("#home").click(function(e) {
    $(".tabs").attr("src","tabs-home.gif");
    $(".islice").hide('fast');
    $(".islice").load("home.html?" + new Date().getTime() );
    $(".islice").show('fast');
    e.preventDefault();
});

This should ensure that a IE isn't caching.

这应该确保 IE 没有缓存。

See http://zacster.blogspot.com/2008/10/jquery-ie7-load-url-problem.htmlfor more info.

有关更多信息,请参阅http://zacster.blogspot.com/2008/10/jquery-ie7-load-url-problem.html

回答by Sukhjeet Singh Mangat

$.ajaxSetup({ cache: false });

$.ajaxSetup({ cache: false });

This will clear cache in IE and .load() will work. I've tried it.

这将清除 IE 中的缓存并且 .load() 将起作用。我试过了。

回答by Neno

If the HTML that you are loading is broken, jQuery's .load() will not work in IE.. It was the problem for me. After I fixed the HTML , everything worked great in IE also !

如果您正在加载的 HTML 损坏,jQuery 的 .load() 将无法在 IE 中工作。这对我来说是个问题。在我修复了 HTML 之后,在 IE 中也一切正常!

回答by Sharad

I encountered this problem and was scratching my head all day long. However finally found a work around and realized what a weirdo IE is.

我遇到了这个问题,整天都在挠头。但是终于找到了解决方法并意识到什么是怪人 IE。

First of all,

首先,

$(".islice").load("home.html"); 

won't work no matter how hard we try. We will instead have to use

无论我们怎么努力都不会奏效。我们将不得不使用

$.get("home.html", function (data) ....... ); 

I will explain the "....." because a usual

我将解释“.....”,因为通常

$.get("home.html", function (data) { $(".islice").html(data); }); // doesn't work

won't work.

不会工作。

Instead

反而

$.get("home.html", function (data) { 
    data = '"' + data + '"';    
    $(".islice").html(data);
    var newHTML = $('.islice').html();
    $('.islice').html(newHTML.substr(1,newHTML.length-2));
}); // works

will work.

将工作。

Explanation: => data may have new line chars. so setting innerHTML = data; breaks because of them. By adding quotes we convert it into a string but making that html adds extra quotes so I get rid of the quotes again.

说明:=> 数据可能有换行符。所以设置innerHTML = data; 因为他们而中断。通过添加引号,我们将其转换为字符串,但使该 html 添加额外的引号,因此我再次摆脱了引号。

Moral: => IE sucks.. Nothing else..

道德:=> IE 很烂.. 没有别的..

回答by Tim Lloyd

I found the .load() function did not work very well with IE, but using $.get() instead worked perfectly, e.g.

我发现 .load() 函数在 IE 上工作得不是很好,但是使用 $.get() 反而工作得很好,例如

var dummy = new Date().getTime();
$.get("home.html" + dummy, function(data) {
   $(".islice").html(data);
});

回答by David Riccitelli

I found this workaround to be working:

我发现此解决方法有效:

$.ajax("loaded.html", {
    cache: false,
    success: function(data, textStatus, jqXHR) {
        $("#content-1").html(data);
    },
    dataType:"html"
});

where:

在哪里:

  • "loaded.html" is the URL to the file to load.
  • $("#content-1") is the element that will contain the loaded data (and scripts).
  • “loaded.html”是要加载的文件的 URL。
  • $("#content-1") 是包含加载数据(和脚本)的元素。

回答by Kevingo Tsai

I think the problem occurred because the ambiguous encoding. Try to specify the response encoding explicitly (that is, the charset in the HTTP Header), something like:

我认为问题的发生是因为编码不明确。尝试明确指定响应编码(即 HTTP Header 中的字符集),例如:

<meta charset="utf-8">

回答by Marek Fajkus

Ok guys... I had same problem with ie 8 and older. This is my solution, hope it helps somebody:

好家伙......我有同样的问题,即 8 岁及以上。这是我的解决方案,希望它可以帮助某人:

1) At first it's pretty hard to debugging ajax in IE. Why? Console is not upto the mark, but there is another bigger problem - caching. First time you load something wrong it stays in cache. Than you spend 2 hours fixing problem seeing same result (when you do this 1st time). Thanks to this article (and discusion): http://zacster.blogspot.cz/2008/10/jquery-ie7-load-url-problem.htmlI customized my ajax calls like this:

1) 起初在 IE 中调试 ajax 非常困难。为什么?控制台不符合标准,但还有另一个更大的问题 - 缓存。第一次加载错误时,它会保留在缓存中。比你花 2 个小时解决问题看到相同的结果(当你第一次这样做时)。感谢这篇文章(和讨论):http: //zacster.blogspot.cz/2008/10/jquery-ie7-load-url-problem.html我像这样定制了我的ajax调用:

$(container).load(link +'?random=' + Math.random()*99999 + ' .post-list li', function() { // do some stuff }

$(container).load(link +'?random=' + Math.random()*99999 + ' .post-list li', function() { // 做一些事情 }

Random URL works great

随机 URL 效果很好

2) @Neno is right! IE have problems with mistakes in HTML. Validate your loading HTML http://validator.w3.org/

2)@Neno 是对的!IE 有 HTML 错误的问题。验证您的加载 HTML http://validator.w3.org/

回答by Tim

I had the same problem with IE9.

我在 IE9 上遇到了同样的问题。

All ajax requests die silently by default. By using http://api.jquery.com/ajaxError/I was able to determine the type of error by logging the exception message: An error with the code c00ce56e.

默认情况下,所有 ajax 请求都会静默消失。通过使用http://api.jquery.com/ajaxError/,我能够通过记录异常消息来确定错误类型:代码为c00ce56e的错误。

It turns out that this means the response is not being passed utf-8 encoded, as it should be in response to an ajax request.

事实证明,这意味着响应没有被传递utf-8 编码,因为它应该响应 ajax 请求。

Turns out I had an typing error in header('Content-type: text/html; charset=utf-8');

原来我有一个打字错误 header('Content-type: text/html; charset=utf-8');

回答by Tomas Aschan

The e.preventDefault()won't make any difference in IE - you'll have to use return false;to stop things from happening:

e.preventDefault()不会使IE浏览器有什么区别-你将不得不使用return false;以阻止发生的事情:

$("#home").click(function(e) {
    $(".tabs").attr("src","tabs-home.gif");
    $(".islice").hide('fast');
    $(".islice").load("home.html");
    $(".islice").show('fast');  
    e.preventDefault();
    return false;
});

To debug this in detail, take a look at Firebug.

要详细调试,请查看Firebug