繁体   English   中英

jQuery .load导入特殊字符

[英]jQuery .load importing special characters

我目前有一个令人难以置信的错误。 我正在使用jQuery中的.load函数加载幻灯片的图像,并通过将URL导入到的img标签在下面得到了很多错误。 我已尽力在下面演示我的功能和标记。 我没有在任何现代浏览器中收到错误,但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/>

如果我对您的理解正确,那么您正在尝试使用jQuery的XMLHttpRequest包装器( load() )预加载图像。 这是不可能的

您所看到的是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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM