2013-07-31 122 views
0

我有一个打印功能,除Safari浏览器以外的所有功能。当点击了打印按钮,则会引发错误:Array.filter破解Safari浏览器打印

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

是打破我的Safari浏览器的代码事情是Array.filter,女巫工程一切,除了野生动物园:

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); 
     }); 
} 
+2

你的函数不应该(也不)在其他浏览器中工作。你想'Array.prototype.filter.call(...)',而不是'Array.filter(...)'。 – georg

+0

thg435,你是坏蛋!谢谢,这是它大声笑。 – user2089255

+2

[数组泛型](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array#Array_generic_methods)仅在FF中可用,我不知道如何可以在任何其他浏览器。 – Bergi

回答

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(); 
+0

这是工作,但它也打开5次打印对话框,然后在打印后崩溃safari。 – user2089255

+0

@ user2089255:你的循环应该做什么? – Ryan

+0

我将整个函数添加到描述中,它将从要打印的列表中填充文本。 – user2089255