javascript - 在 IE7/IE8 中获取尾随逗号(在 JavaScript 中)

标签 javascript internet-explorer-7

您可以在这里找到很多关于尾随逗号的信息: Are trailing commas in arrays and objects part of the spec?

这是关于“就在那里”的逗号。

[
        { title: "..", display: function() { return ".."; } },
        { title: "..", display: function() { return ".."; } },
        { title: "..", display: function() { return ".."; } }, // <--right there
    ]

我现在的问题是,第三方工具生成此列表时带有逗号,我无法访问该工具的源代码。我

<body>
my stuff
<!-- the folloging div with the javascript comes from the 3rd party -->
<div class="item"><script type="text/javascript">
$(document).ready(function() {


    $.supersized({

        // Functionality
        slide_interval          :   8000,       // Length between transitions
        transition              :   1,          // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
        transition_speed        :   400,        // Speed of transition
        fit_always              :   0,          //Prevents the image from being cropped by locking it at 100% width.
        min_width               :   600,        //min-width of images
        random                  :   0,      //random img
        vertical_center         :   1,          //vertical alignment

        // Components                           
        slide_links             :   'false',    // Individual links for each slide (Options: false, 'number', 'name', 'blank')
        slides                  :   [           // Slideshow Images

    {image : 'image1.jpg', otherstuff: 'blah', moreotherstuff: 'lorem'},

    {image : 'image2.jpg', otherstuff: 'blub', moreotherstuff: 'ipsum'},


                                    ]
    });
});
</script></div>

现在我问我有没有办法让它在 IE7/IE8 中工作。

我现在看到的唯一方法(因为我无法编辑第 3 方并且无法在服务器上访问它)是: - 给出一个没有那个 div 的特殊输出 - 通过ajax请求正常端 - 解析所有内容,获取此 div,删除最后一个逗号并执行 javascript

这确实不是我想要的方式。还有其他方法可以解决这个问题吗?

最佳答案

IE7/IE8 将报告 1 个元素更大的数组长度,除非您 trim 这些数组。

如果您知道所有接受来自第三方的数据的 JS 方法,您可以为它们编写包装器。例如:

var removeEmpty = function(array){
    var i, result = [];

    for (i = 0; i < array.length; ++i){
        if ( typeof array[i] != 'undefined' ){
            result.push(array[i]);
        }
    }

    return result;
};

$.supersized = (function() {
    var origMethod = $.supersized;

    return function(config) {
        config.slides = removeEmpty(config.slides); // get rid of undefined data in array
        return origMethod.call(this, config); // call original method
    };
}());

关于javascript - 在 IE7/IE8 中获取尾随逗号(在 JavaScript 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19450114/

相关文章:

javascript - 在创建的钩子(Hook)中使用方法会给出引用错误

javascript - 如何使用过渡动画使 div 居中

javascript - Swiper - OpenCart - 背景全尺寸图像不起作用

css - Internet Explorer 框模型 - 什么是偏移量?

javascript - .html() 在 Internet Explorer 中不起作用

css - <button> 标签有什么缺点?

javascript - 如何在 jquery 中获取 XMLHttpRequest 对象,以便稍后用于另一个请求?

javascript - 如果每天单击一次按钮并在第二天使用 JavaScript 和 AngularJS 1 启用,如何禁用按钮?

javascript - 该正则表达式的哪一部分导致它在 IE7 和 IE8 中找不到匹配项?

css - IE7 中未对齐的滑动门