jQuery .load导入特殊字符

时间:2010-08-27 02:09:27

标签: javascript jquery ajax html5 character-encoding

我目前有一个令人难以置信的错误。我在jQuery中使用.load函数来加载幻灯片的图像,并通过img标记获取一堆错误,我将导入url。我已尽力展示我的功能和标记如下。我在任何现代浏览器中都没有出错,但IE给了我错误

     $('a').click(function(e){
    $(trainFull).fadeOut(400, function(){
         jQuery(trainFull[0]).load('http://img.jpg", function(){
          jQuery(this).hide().attr('src', 'http://img.jpg").fadeIn(400);
   });



<img alt="Train Full" src="http://img.jpg" ">
<img xmlns="http://www.w3.org/1999/xhtml">�����JFIF���d�d�����Ducky�����(�����Adobe�d���������--etc, etc, etc/>

1 个答案:

答案 0 :(得分:1)

如果我理解正确,那么您正尝试使用jQuery的XMLHttpRequest包装器(load())预加载图像。这是...... unlikely to work well

你所看到的是IE试图将二进制图像数据解释为文本(具有可预测的差的结果),jQuery试图将它们填充到你的<img>元素中,并且IE试图显示它们。虽然这可能会设法预先缓存图像,但这是工作的错误工具......正如您所演示的那样,可能会严重失败的工具

幸运的是,有更简单的方法。大多数浏览器都提供内置支持,用于加载图像并在加载时通​​知脚本。例如:

$('<img />') // detached image, used to pre-load
    .attr('src', 'http://img.jpg') // tell it which image to load
    .load(function() // attach a callback to inform us when it's loaded
    {
      // within the callback, "this" refers to the temporary image element
      // that has just finished loading its source

      // now that the image is in the cache,
      $(trainFull[0])
        .attr('src', this.src) // let our on-page image display it
        .fadeIn(400); // and show that
    });

如果您想了解更多相关信息,建议您从jQuery ajax images preload开始。

相关问题