简体   繁体   English

如何在另一个 div 中使用 HTML2canvas 显示捕获的图像

[英]How to display captured image with HTML2canvas in another div

Currently when i press the button, the captured image will be displayed in the body of the HTML.目前,当我按下按钮时,捕获的图像将显示在 HTML 的主体中。 But how do I make it that the captured image will only be displayed in another div with the id of "image".但是如何使捕获的图像仅显示在另一个具有“图像” id 的 div 中。 I tried using document.getElementById("image").appendChild(canvas) but it did not work.我尝试使用 document.getElementById("image").appendChild(canvas) 但它不起作用。 Need to do this as I'm planning to hide the element first by using getElementById("image").="none" and it'll only appear after a click of a button.需要这样做,因为我计划首先使用 getElementById("image").="none" 隐藏元素,并且它只会在单击按钮后出现。

HTML code: HTML 代码:

    <div>
       <table id="capture">                 
                <tr id="date" class="selected">
                   <th>Date</th>
                   <th>11/5</th>                  
                   <th>12/5</th>
                   <th>13/5</th>
                   <th>14/5</th>
                   <th>15/5</th>
                   <th>16/5</th>
                   <th>17/5</th>

                </tr>
                <tr class="selected">
                    <th>Day</th>                  
                    <th>Mon</th>
                    <th>Tue</th>
                    <th>Wed</th>
                    <th>Thu</th>
                    <th>Fri</th>
                    <th>Sat</th>
                    <th>Sun</th>
                </tr>           
            </table>

          <input type="button" value="Last Week's Dosage History" class="submitonclick="showlastweek()"/> 
            <button type="button" class="submit" onclick="capture1()">Capture<small></small></button>
    </div> 

    <div id="image">
    //image to be placed here
    </div>

Js code: js代码:

function capture1(){
    html2canvas(document.querySelector("#capture")).then(canvas => {
    document.body.appendChild(canvas)
});
}

Here you go.这里是 go。 Tested and working.测试和工作。

document.getElementById("image").appendChild(canvas); Works just fine... But getElementById("image").="none" means nothing.工作得很好......但是getElementById("image").="none"没有任何意义。 See below.见下文。

 function capture1() { html2canvas(document.querySelector("#capture")).then((canvas) => { document.getElementById("image").appendChild(canvas); }); } function showDiv() { var hiddenDiv = document.querySelector("#image"); hiddenDiv.style.display = hiddenDiv.style.display === "none"? "": "none"; }
 <div> <table id="capture"> <tr id="date" class="selected"> <th>Date</th> <th>11/5</th> <th>12/5</th> <th>13/5</th> <th>14/5</th> <th>15/5</th> <th>16/5</th> <th>17/5</th> </tr> <tr class="selected"> <th>Day</th> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> <th>Sun</th> </tr> </table> <input type="button" value="Last Week's Dosage History" class="submit" onclick="showlastweek()"/> <button type="button" class="submit" onclick="capture1()"> Capture<small></small> </button> </div> <div id="image" style="display: none;"> //image to be placed here </div> <br /> <br /> <button type="button" class="submit" onclick="showDiv()"> Toggle div display </button>

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

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