在新页面中打开图像,单击时打印

Open images in a new page and print when clicked

本文关键字:单击 打印 图像 新页面      更新时间:2023-09-26

这是我的页面:http://budclarychevy.com/custom/parts-specials-test

我的目标是使图像可单击并在新窗口中打开,并提示打印。我能找到的最接近的方法就是 JavaScript 中的 window.print() 函数,但它只打印整个页面。我希望每个图像都单独在可打印的页面上打开。这在 JavaScript 中可能吗?如果有另一种方法,那是什么?

你可以简单地添加一个带有一些JavaScript的onclick事件。

<img src="http://budclarychevy.com/path/to/my/image.png"
width="660" height="225" alt="GM Oil Filters"
onclick="newWindow = window.open('http://budclarychevy.com/path/to/img.png');
    newWindow.print();">

如您所见,单击后会打开一个新窗口,其中包含图片的 url。由于您有对新窗口对象的引用,因此可以对其调用"print()"函数。

干杯悠闲

你可以

做类似的事情

function clicked(address) {
    popup = window.open(); // display popup
    popup.document.write(address.src); // This is where the image url goes which will just open up the image
    popup.print(); // then print the image
}

在 HTML 中

<img src="image.jpg" onclick="clicked(this)" />