简体   繁体   English

jQuery.each()在Firefox和IE中不起作用?

[英]jQuery.each() not working in Firefox and IE?

I have a problem with using $.each function between browsers. 我在浏览器之间使用$ .each函数时遇到问题。

I have lists with background-images and if they are finished loading the parent DIV will fadeIn. 我有背景图像列表,如果完成加载,父DIV将淡入。

I dont know if my code is correctly written (but no js error in console). 我不知道我的代码是否写得正确(但在控制台中没有js错误)。 This code works fine in google chrome but not in Firefox and IE(especially in ver.8 and .9). 此代码在Google Chrome中运行良好,但在Firefox和IE中则无效(特别是在ver.8和.9中)。

HTML: HTML:

<ul id="slides">
<li id="slides1"><p><a href="#"><img src="http://placekitten.com/g/200/100" alt=""></a></p></li>
<li id="slides2"><p><a href="#">No.2</a></p></li>
<li id="slides3"><p><a href="#">No.3</a></p></li>
<li id="slides4"><p><a href="#">No.4</a></p></li>
<li id="slides5"><p><a href="#">No.5</a></p></li>
</ul>

sorry 'bout messy code. 对不起'关于凌乱的代码。

JS: JS:

var _imgCounter = 0;
var _parent = $('#slides');
var _child = $('#slides li');
var _childL = _child.length;
var _imgURLArr = [];
var _imgURL;
var _dummyImg;

_child.each(function() {
    _imgURL = $(this).css('background-image');
    _imgURL = _imgURL.substring(4, _imgURL.length - 1);//omit 'url()'
    _imgURLArr.push(_imgURL);
    console.log(_imgURL);
});
console.log(_imgURLArr.length);
$.each(_imgURLArr, function(i,val) {
    var _tempImg = $('<img/>').attr('src',val);
    _tempImg.one('load', function() {
        console.log(val + 'is loaded!');
        _imgCounter++;
        console.log('counter is ' + _imgCounter);
        if(_imgCounter == _childL) {
            _parent.fadeIn('fast');
        }
    });
    if(_tempImg.complete) {
        _tempImg.trigger('load');
    }
});

DEMO: http://jsfiddle.net/nori2tae/g8MHs/ 演示: http//jsfiddle.net/nori2tae/g8MHs/

I referred to load technique here: http://goo.gl/971A9 我在这里提到了加载技术: http//goo.gl/971A9

This doesn't seem to fire $.each function in FF and IE. 这似乎没有在FF和IE中激发$ .each功能。

Need some solution. 需要一些解决方案

Thank you. 谢谢。

Your problem is not with the each function, but rather the fact the the URL has quotes in it. 问题不在于each函数,而在于URL中有引号的事实。 That is, the value of the string is 也就是说,字符串的值是

"http://dummyimage.com/640x400/0f0/fff.png" , "http://dummyimage.com/640x400/0f0/fff.png"

and when setting the image URL, Firefox parses it and then treats as a relative URL and tries to access 并且在设置图像URL时,Firefox会对其进行解析,然后将其视为相对URL并尝试访问

http://fiddle.jshell.net/nori2tae/g8MHs/show/%22http://dummyimage.com/640x400/0f0/fff.png%22

which results in a 404, which means the load handler is never called. 这导致404,这意味着永远不会调用负载处理程序。

To fix: 修理:

change 更改

_imgURLArr.push(_imgURL);

to

_imgURLArr.push(_imgURL.replace(/"+/g,""));

to eliminate the quotes. 消除报价。

Here's the updated working jsFiddle example 这是更新的工作jsFiddle示例

I also suggest adding a handler for when the image fails to load, that would save you some hair pulling when trying to debug your implementation of the script :) 我还建议在图像无法加载时添加一个处理程序,这样可以在尝试调试脚本的实现时省去一些头发:)

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

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