我有一個代碼,這是打印一個特定的div,當點擊一個鏈接,但我似乎無法得到它的工作。Javascript的打印代碼不工作
代碼的目的是,當我點擊鏈接「HEJ」它應該打印我的div,稱爲mydiv,並且我找到了一個腳本,應該這樣做,但我似乎無法讓它工作。
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script>
<script type="text/javascript">
function PrintElem(elem)
{
Popup($(elem).html());
}
function Popup(data)
{
var mywindow = window.open('', 'my div', 'height=400,width=600');
mywindow.document.write('<html><head><title>my div</title>');
/*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
mywindow.document.write('</head><body><p style="font-family:arial">');
mywindow.document.write(data);
mywindow.document.write('</p></body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print();
mywindow.close();
return true;
}
</script>
上面的代碼是我在互聯網上找到的JavaScript,下面的代碼是觸發javascript代碼的HTML。
<div id="mydiv">
Test
</div>
<a href="#" onclick="PrintElem('.mydiv')" />HEJ</a>
我也嘗試刪除。在onclick元素的mydiv之前,但沒有任何作用,當我點擊鏈接時沒有任何反應。
我希望你們能幫我解決這個問題。
我還沒有完全通過您的代碼了,但你可以嘗試用‘#mydiv’取代「.mydiv」,看看它是否工作 –
Onfortunately它沒有工作:) –
請籤@jedifans答案 –