2015-04-06 47 views
1

爲了在我的頁面框架上打印一組數據,我創建了一個javascript function以將其設置爲a標記上的onClick。但是,該功能只是在點擊打印時僅獲得一個參數,而不是全部。將兩個函數參數應用於onClick標記

所以我做了HTML:

<div id="services">Services available</div> 
<div id="products">Products available</div> 

和JavaScript函數(兩個參數):

function Popup(data1, data2) { 
    var printWindow = window.open('', 'Page', 'width=600,height=600,left=400'); 
    printWindow.document.write(data1); 
    printWindow.document.write(data2); 

    return true; 
} 

function PrintElem(elem1, elem2) { 
    Popup($(elem1, elem2).html()); 
} 

而且a標籤設置爲被點擊並打開一個彈出窗口。窗戶被打開,與提供的服務,但產品不會出現和輸出undefined

<a onClick="PrintElem('#services, #products')">Print page</a> 

我怎樣才能讓閱讀功能都IDS?

回答

6

你傳入了一個,不是兩個參數

"PrintElem('#services, #products')" 

登錄console.log(elem1);將顯示字符串"#services, #products"

應該

"PrintElem('#services', '#products')" 

下一個問題是事實,

Popup($(elem1, elem2).html()); 

使用elem2作爲上下文選擇器,它不查找這兩個元素的html。你的函數需要兩個參數,以便將它傳遞了HTML字符串

function PrintElem(elem1, elem2) { 
    Popup($(elem1).html(), $(elem2).html()); 
} 
+0

抓住了我,因爲我是編輯它@KJPrice :)看準那之後我做了回答第一個問題。 – epascarello