For 循环中的 Javascript If 语句适用于 Chrome 和 Safari,但不适用于 Firefox

标签 javascript jquery firefox if-statement

作为 TeamTreehouse.com 竞赛学习过程的一部分,我正在学习编码和构建图片库。

这是代码:http://jsfiddle.net/kylenoble/8Wf8r/以及运行代码时的结果:http://jsfiddle.net/kylenoble/8Wf8r/embedded/result/ .

我遇到的问题是单击嵌套在 .item div 中的 .i div。在我的头撞墙几个小时后,我将它缩小到下面的 if 语句在 Chrome 和 Safari 中触发但在 Firefox 中不触发。

具体代码如下:

    //Loops through the infoUrl's and populates the info button with
    //infoUrl that matches the imageUrl

    for(i = 0, len = (picturesList.length - 1); i <= len; i ++) {
        var infoUrl = picturesList[i].infoUrl;
        var imageUrl = picturesList[i].imageUrl;
        var displayImage = 'url(' +imageUrl+ ')';
        var currentItemUrl = ($(this).css('background-image'));

        //Matches the imageUrl in the displayImage var with the  
        //corresponding infoUrl and opens the matched infoUrl when 
        //the 'info' button is clicked. 

        if (displayImage === currentItemUrl) {
            console.log('true');
            $('.i').click( function(event) {
                window.open(infoUrl);
                event.stopImmediatePropagation();
            });
        break;
        }
    }

我似乎不明白为什么 if 语句不会在 Firefox 中触发,但会在 Chrome 和 Safari 中触发。非常感谢您可能提供的任何帮助,如果我在这个问题的任何一点上都不清楚,我深表歉意。

谢谢!

最佳答案

我已经检查了 Firefox 中的调试器,它显示了 displayImagecurrentItemUrl 之间的差异。

currentItemUrl: "url("http://media.nara....ges/16/7/16-0651a.gif")"

displayImage: "url(http://media.nara.g...ges/14/14/14-1336a.gif)"

注意引号。我不确定为什么,但 Firefox 似乎以不同的方式返回 css 属性。

关于For 循环中的 Javascript If 语句适用于 Chrome 和 Safari,但不适用于 Firefox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21460247/

相关文章:

php - 在 $.ajax jQuery 中返回 false 成功

php - 更改数据库中已保存的信息以使用另一个 php 更新

c# - 如何检查浏览器是否打开

javascript - 以编程方式对 Chrome 堆进行快照

javascript - HTML/CSS/JS 中未捕获的类型错误

javascript - 使用js进行表单验证冲突

javascript - Facebook 页面插件无法在网站上正确显示

html - 图标在 Chrome 和 Firefox 中居中,但在 Safari 中不居中

python - 接收属性错误 'FirefoxBinary'对象没有属性 '_get_firefox_output'

javascript - 如何使用 Google Analytics 跟踪 A/B 测试中的社交参与度?