在一個腳本中,我有
<a href="javascript:void;" id="druck">
這是由 javascript 在另一個腳本中評估的(這些是 django 中的嵌套模板)。然后,這應該會打開一個新視窗,以便準備一個對列印機更友好的站點(還有一段路要走)。
$(function() {
var b = document.getElementById("druck");
b.onclick = function() {
var mywindow = window.open('', 'PRINT', 'height=400,width=600');
mywindow.document.write('<html><head><title>' document.title '</title>');
mywindow.document.write('<link rel="stylesheet" href="{% static 'boot/css/bootstrap.min.css' %}" type="text/css">');
mywindow.document.write('<link rel="stylesheet" href="{% static 'boot/css/bootstrap.css' %}" type="text/css">');
mywindow.document.write('<link rel="stylesheet" href="{% static 'boot/css/dashboard.css' %}" type="text/css">');
mywindow.document.write('</head><body>');
我想將第一個腳本的字串引數傳遞給第二個腳本,這樣我也可以列印字串。我不適合 js,因此我問如何將字串傳遞給帶有 id 的 href 和帶有 onclick 等的 javascript void 函式。
uj5u.com熱心網友回復:
您可以通過 2 種方式進行操作。您可以從元素本身呼叫該函式,例如:
<a href="javascript:void;" onclick="myFunction ('string')" id="druck">
或者您可以使用 data-* 屬性;
<a href="javascript:void;" data-string="my string" id="druck">
而在js里面,
b.onclick = (event) => {
let myString = event.currentTarget.dataset.string;
//Code
}
( https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset )
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/419652.html
標籤:
下一篇:陣列回圈不顯示所有資料?
