2013-07-31 132 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