在新窗口中打开图片

IT技术 javascript image
2021-02-24 18:40:05

如何使用它在新窗口中打开图像id

function swipe()
{   
    var largeImage = document.getElementById('largeImage');
    largeImage.style.display = 'block';
    largeImage.style.width=200+"px";
    largeImage.style.height=200+"px";                   
}

单击图像时调用此函数。现在,它在同一个窗口中打开,但我想在新窗口中打开它。如何做到这一点?

6个回答
function swipe() {
   var largeImage = document.getElementById('largeImage');
   largeImage.style.display = 'block';
   largeImage.style.width=200+"px";
   largeImage.style.height=200+"px";
   var url=largeImage.getAttribute('src');
   window.open(url,'Image','width=largeImage.stylewidth,height=largeImage.style.height,resizable=1');
}

HTML代码:

<img src="abc.jpg" onClick="swipe();"/>

对于很可能在主窗口后面丢失的新窗口,并且通常会惹恼访问者:

window.open('http://example.com/someImage.png');

如果我是你,我会坚持使用常规链接。

目前在 Chrome 上,我必须刷新才能看到图像。也使用绝对 URL
2021-04-25 18:40:05
为什么你会假设他正在处理面向客户的代码而不是调试工具或构建一个非常有用的特定用途的功能?他问了一个抽象的问题。他应该得到一个抽象的答案。
2021-05-06 18:40:05

尝试:

<img src="URL or BASE64" onclick="window.open(this.src)">
我从 Chrome 收到此错误: Not allowed to navigate top frame to data URL
2021-04-29 18:40:05

尝试使用以下功能:

function newTabImage() {
    var image = new Image();
    image.src = $('#idimage').attr('src');

    var w = window.open("",'_blank');
    w.document.write(image.outerHTML);
    w.document.close(); 
}

使用此 HTML 代码调用:

<img id="idimage" src="data:image/jpg;base64,/9j/4A.." onclick="newTabImage()">

HTML:

<input type="button" onclick="test()" value="test">

JavaScript

    function test(){
    url = "https://www.google.de//images/branding/googlelogo/2x/googlelogo_color_272x92dp.png";
    img = '<img src="'+url+'">';
    popup = window.open();
    popup.document.write(img);                        
    popup.print();
    }

试试这个:https : //jsfiddle.net/ne6f5axj/10/

您必须将图像的 url 放在图像标签中。