简体   繁体   English

有人告诉我为什么这个Jquery在IE中不起作用吗?

[英]Anyone tell me why this Jquery doesn't work in IE?

The following code works fine in FF chrome etc but not IE, anyone know why? 下面的代码在FF chrome等中可以正常工作,但在IE中则不能,有人知道为什么吗?

Basically everything works accept the replacement of the image attribute 基本上所有的工作都可以接受image属性的替换

$("#container #main_image img#largeId").attr({ src: largePath });

Even the rest of the function ie the swapping classes works, just not the image replacement. 甚至该功能的其余部分(即交换类)都可以工作,而不是图像替换。 The full example can be seen here example click on an image and try and swap between the thumbnails on the right in the modal window. 完整的示例可以在此处看到, 示例单击图像,然后尝试在模式窗口右侧的缩略图之间切换。

The effect i'm going for is similar to that from this website webdesignerwall , the jQuery is almost exactly the same - although this works with IE!!!! 我要达到的效果与本网站webdesignerwall的效果相似,jQuery几乎完全相同-尽管它可以与IE一起使用!!!!

jQuery(document).ready(function($) {

  $("a.group").fancybox({
    'frameWidth':966,
    'frameHeight': 547,
    'hideOnContentClick': false,
    'overlayOpacity': 0.85,
    'callbackOnShow': function() {
      $("#container ul#thumbnails li a").click(function(){
        var largePath = $(this).attr("title");
        $("#container #main_image img#largeId").fadeOut().hide();
        $("#container #main_image img#largeId").attr({ src: largePath });
        $("#container #main_image img#largeId").fadeIn("slow");
        $('.active').removeClass('active');
        $(this).addClass("active");return false;
      });
    }
  });

});

Ok, I have worked it out. 好的,我已经解决了。 it was painfull and I conclude that the plugin is plain wrong:) 这很痛苦,我断定该插件是完全错误的:)

The plugin duplicates the hidden content and inserts it into the 'box'. 插件将复制隐藏的内容并将其插入“框”中。 This means you end up with duplicate id's everywhere which of course is invalid and funny things start to happen. 这意味着您到处都是重复的ID,这当然是无效的,并且有趣的事情开始发生。 When you do you selector for $('#largeId') it actually references the original hidden photo. 选择$('#largeId')时,它实际上引用的是原始隐藏的照片。 You can see this if you remove the display:none style from #hidden. 如果您从#hidden中删除display:none样式,则可以看到此内容。

Conclusion is to use a lightbox that actually appends the content rather than duplicates it. 结论是使用一个实际上附加内容而不是重复内容的灯箱。 I do not like what the author has done hear. 我不喜欢作者的所作所为。

You could amend the plugin code to use append instead of using (line 112 of the plugin) like this 您可以修改插件代码以使用append而不是使用(插件的第112行),如下所示

 var newHtml = $('<div id="fancy_div" />').append(target);
 _set_content(newHtml.html(), opts.frameWidth, opts.frameHeight);

You will then have to deal with putting the html back when the lightbox is closed. 然后,您必须处理在关闭灯箱时放回html的问题。 However I would be more inclined to find a better lightbox! 不过,我会更倾向于寻找更好的灯箱!

Have you used Fiddler to see if the image is being requested? 您是否曾使用Fiddler来查看是否正在请求图像?

This always works for me: $("#foo").attr("src",url); 这总是对我有用:$(“#foo”)。attr(“ src”,url);

You could also just do a replacement instead. 您也可以代替。

$('#foo').before('<img src="" ... />').remove(); $('#foo')。before('<img src =“” ... />')。remove();

The paths listed in the title attribute of your <a> tags are incorrect (check case). <a>标记的title属性中列出的路径不正确(请检查大小写)。 Specifically, 特别,

<a class="group" href="#hidden" title="images/2_large.jpg">

should be 应该

<a class="group" href="#hidden" title="Images/2_large.jpg">

Firefox is a little more forgiving, but IE is rejecting such shenanigans. Firefox稍宽容一些,但IE拒绝了这种恶作剧。

In addition to BipedalShark's answer, I noticed a few things that sorta stuck in my mind: 除了BipedalShark的答案外,我还注意到了一些我想起的事情:

  1. You're calling jquery.min.js from google up in the head of your page. 您是在页面顶部从Google调用jquery.min.js。
  2. Later on, you're calling fancybox.js near the bottom of the page. 稍后,您将在页面底部附近调用fancybox.js。
  3. Finally, you're calling another jquery.js after it. 最后,您要在其后调用另一个jquery.js。

So I know (as well as a possible solution) is it possible that this is partially responsible for the problem? 因此,我知道(以及可能的解决方案)这可能是造成问题的部分原因吗? Or, does fancybox need to be loaded at the bottom of the page? 或者,是否需要在页面底部加载fancybox?

Basically this has been resolved by changing the destination element from an 'ID' to a 'class'. 基本上,已通过将目标元素从“ ID”更改为“类”来解决此问题。

This needs to be done because of Fancybox duplicating the id when calling the modal window! 之所以需要这样做,是因为Fancybox在调用模式窗口时会复制ID!

Many thanks for everyones helps here! 非常感谢大家的帮助!

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

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