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

时间:2012-06-14 22:36:36

标签: javascript html javascript-events

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

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

2 个答案:

答案 0 :(得分:8)

你只需用一些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();">

如您所见,点击后会打开一个新窗口,其中包含图片的网址。当您有对新窗口对象的引用时,可以在其上调用'print()'函数。

干杯 悠闲

答案 1 :(得分:0)

您可以执行类似

的操作
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)" />
相关问题