在onClick标记中应用两个函数参数

Cai*_*ari 1 javascript jquery arguments onclick

为了打印一组数据,在我的网页框架,我创建一个JavaScript function将其设置为onClick一个a标签.但是,该功能只是在单击打印时只获得一个参数,而不是所有参数.

所以我制作了HTML:

<div id="services">Services available</div>
<div id="products">Products available</div>
Run Code Online (Sandbox Code Playgroud)

和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());
}
Run Code Online (Sandbox Code Playgroud)

a标签被设置为被点击并打开一个弹出窗口.窗口打开,服务可用,但产品不显示和输出undefined

<a onClick="PrintElem('#services, #products')">Print page</a>
Run Code Online (Sandbox Code Playgroud)

如何让函数读取两个ID?

epa*_*llo 6

你传递了一个,而不是两个参数

"PrintElem('#services, #products')"
Run Code Online (Sandbox Code Playgroud)

日志记录console.log(elem1);将显示字符串"#services, #products"

应该

"PrintElem('#services', '#products')"
Run Code Online (Sandbox Code Playgroud)

下一个问题是

Popup($(elem1, elem2).html());
Run Code Online (Sandbox Code Playgroud)

使用elem2作为上下文选择器,它没有查找两个元素的html.你的函数期望两个参数,所以传递给它两个html字符串

function PrintElem(elem1, elem2) {
  Popup($(elem1).html(), $(elem2).html());
}
Run Code Online (Sandbox Code Playgroud)