简体   繁体   English

在保存对话框中使用image.jpg而不是true.html

[英]image.jpg instead of true.html in save dialog box

I don't know how to add image in my snippet code but hope the question will bi clear anyway. 我不知道如何在代码段中添加图片,但希望无论如何该问题都会清除。

Want to save ie download imgb on disk. 要保存,即在磁盘上下载imgb

Save As dialog appears but: 出现Save As对话框,但是:

  • default name and format is true.html - I want it to be image.jpg or similar. 默认名称和格式为true.html我希望它为image.jpg或类似名称。

  • there is no any existing image in the list so I can't click and replace it with the new one. 列表中没有任何现有图像,因此我无法单击并将其替换为新图像。

  • if I write image.jpg inside Save As dialog the final result is not an image but a black rectangle. 如果我在“ Save As对话框中写入image.jpg则最终结果不是图像而是黑色矩形。

Any help? 有什么帮助吗?

 $('button').on('click', function(){ let dl = document.createElement("a"); dl.href = document.getElementById('imgb'); dl.download = true; document.body.appendChild(dl); dl.click(); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <img class='imgb' id='imgb' src='img.jpg' alt='img'> <br><br> <button>CLICK</button> 

I think this will work: 我认为这会起作用:

 $('button').on('click', function(){
      let dl = document.createElement("a");
      dl.href = document.getElementById('imgb').getAttribute("src");
      dl.download = true;
      document.body.appendChild(dl);
      dl.click();
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

html: HTML:

    <img id='imgb' src='img.jpg' alt='img'>
    <br><br>
    <button>CLICK</button>

Your element hat the class of imgb and not id, and you can access the href by using getAtribute 您的元素是imgb而不是id的类,您可以使用getAtribute访问href

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM