亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

打印 img 源不是 img 本身不起作用,當來自本地主機的 img 時

打印 img 源不是 img 本身不起作用,當來自本地主機的 img 時

POPMUISE 2023-10-17 15:48:05
當用戶單擊圖像時,我正在嘗試打印圖像。當來自互聯網的 img 時它可以工作,但當它來自我自己的電腦(本地主機)時它不起作用。但是為什么?我嘗試了很多腳本,但都給了我相同的結果。提前致謝<!DOCTYPE html><html><head></head><body>        <div id="tab"><img style="width:600px; hieght:600px;" src="https://scontent.fgza6-1.fna.fbcdn.net/v/t1.15752-9/90511757_144802246885621_238915900961456128_n.jpg?_nc_cat=111&_nc_sid=b96e70&_nc_ohc=r3aJA4RgmrIAX9uj7MO&_nc_ht=scontent.fgza6-1.fna&oh=bc1a6cdf63922303eb725277d019cffa&oe=5EA69107" alt="Bald Eagle" />        </div>     <div id="tab2"><img style="width:600px; hieght:600px;" src="http://localhost:8000/storage/images/VW5wb8YGHetqZy8BKcnQuCVGcJwqXgakKViyeuoV.jpeg" alt="Bald Eagle" />        </div>    <p>        <input type="button" value="Print img" onclick="myApp.print()" /> <!-- it's work -->        <input type="button" value="Print img local" onclick="myApp2.printLocal()" /> <!-- it not work !! -->    </p></body><script>    var myApp = new function () {        this.print = function () {            var tab = document.getElementById('tab');            var win = window.open('', '', 'height=508,width=580');            win.document.write(tab.outerHTML);            win.document.close();            win.print();        }    }    var myApp2 = new function () {        this.printLocal = function () {            var tab = document.getElementById('tab2');            var win = window.open('', '', 'height=508,width=580');            win.document.write(tab.outerHTML);            win.document.close();            win.print();        }    }</script></html>
查看完整描述

1 回答

?
元芳怎么了

TA貢獻1798條經驗 獲得超7個贊

我已經在本地主機上嘗試了您的代碼,它運行良好,我確實給了而不是src="/storage/images/VW5wb8YGHetqZy8BKcnQuCVGcJwqXgakKViyeuoV.jpeg"src="http://localhost:8000/storage/images/VW5wb8YGHetqZy8BKcnQuCVGcJwqXgakKViyeuoV.jpeg"


試試運氣


從文件上載打印


.HTML


<input type="file" accept="img/*" onchange="fileChanged(event)" /> 

.JS


function fileChanged(e){

  let file=e.target.files[0];  document.querySelector('#tab2>img').src=window.URL.createObjectURL(file); 

}

<!DOCTYPE html>

<html>

<head>

</head>

<body>

        <div id="tab"><img style="width:100px; height:100px;" src="https://scontent.fgza6-1.fna.fbcdn.net/v/t1.15752-9/90511757_144802246885621_238915900961456128_n.jpg?_nc_cat=111&_nc_sid=b96e70&_nc_ohc=r3aJA4RgmrIAX9uj7MO&_nc_ht=scontent.fgza6-1.fna&oh=bc1a6cdf63922303eb725277d019cffa&oe=5EA69107" alt="Bald Eagle" />

        </div> 



    <div id="tab2"><img style="width:100px; height:100px;" src="" />

        </div>


    <p>

        <input type="button" value="Print img" onclick="myApp.print()" /> <!-- it's work -->

        <input type="file" accept="img/*" onchange="fileChanged(event)" /> 

        <input type="button" value="Print img local" onclick="myApp2.printLocal()" /> <!-- it not work !! -->

    </p>

</body>

<script>

    var myApp = new function () {

        this.print = function () {

            var tab = document.querySelector('#tab>img');

            var win = window.open('', '', 'height=508,width=580');

            win.document.write(tab.outerHTML);

            win.document.close();

            win.print();

        }

    }

    function fileChanged(e){

      let file=e.target.files[0];  document.querySelector('#tab2>img').src=window.URL.createObjectURL(file); 

    }

    var myApp2 = new function () {

        this.printLocal = function () {

            var tab = document.querySelector('#tab2>img');

            var win = window.open('', '', 'height=508,width=580');

            win.document.write(tab.outerHTML);

            win.document.close();

            win.print();

        }

    }

</script>

</html>


查看完整回答
反對 回復 2023-10-17
  • 1 回答
  • 0 關注
  • 125 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號