2015-04-06 50 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