Array.filter打破了safari打印

时间:2013-07-31 21:23:00

标签: javascript jquery

我有一个打印功能,除了safari之外的所有功能。单击打印按钮时,将引发错误:

TypeError: 'undefined' is not a function (evaluating 'Array.filter( document.getElementsByClassName('printArea_1'), function(elem){
        $(".printing_list").printElement( elem );
    })')

破坏我的safari代码的是一个Array.filter,除了safari之外,还可以使用其他所有东西:

Array.filter( document.getElementsByClassName('printArea_1'), function(elem){
    $(".printing_list").printElement( elem );
});

我已经尝试添加了一大堆代码,这些代码应该可以使用safari,但却没有。任何人都可以帮助我解决这个问题,或者帮助我写一些可以用所有浏览器中的东西代替它的东西吗?

这是我的完整打印功能

function print_list(item_names,number_of_items) {
    var theText="<ol>";
    for(var i=1; i<=number_of_items;i++){
        if($("#" + item_names + "_" + i).val()!=''){
        theText+="<li>"
        theText+=$("#" + item_names + "_" + i).val();   
        theText+="</li>";

        }

    }
    theText +="</ol>";
    $("#print_content_area").html(theText);
        Array.prototype.filter.call( document.getElementsByClassName('printArea_1'), function(elem){
            $(".printing_list").printElement( elem );
        });
}

1 个答案:

答案 0 :(得分:2)

我不确定这在其他浏览器中是如何工作的。 Array.filter不存在;您的polyfill会创建 Array.prototype.filter ,这是正确的功能。您可以使用.call使其适应类似数组的对象:

Array.prototype.filter.call( document.getElementsByClassName('printArea_1'), function(elem){
    $(".printing_list").printElement( elem );
});

filter不是正确的功能; forEach是。

而且......你有jQuery吗?

$('.printArea_1').each(function() {
    $('.printing_list').printElement(this);
});

您似乎也应该缓存$('.printing_list')


所以你想这样做?

$('.printing_list').append($('.printArea_1')).printElement();