Javascript 单击按钮后如何将svg转换为base64

Javascript 单击按钮后如何将svg转换为base64,javascript,jquery,html,svg,Javascript,Jquery,Html,Svg,Html和svg <svg class="teeth" width="400px" height="300px" viewBox="0 0 400 300" preserveAspectRatio="xMidYMid meet"> <!-- upper right 8 --> <g id="molar-group" class="molar"> <rect x="75" y="

Html和svg

     <svg class="teeth" 
     width="400px" height="300px" viewBox="0 0 400 300" preserveAspectRatio="xMidYMid meet">
        <!-- upper right 8 -->
        <g id="molar-group" class="molar">
            <rect x="75" y="75" stroke="black" id="disto-occlusal" style="stroke-width: 5px;" width="125" height="150" fill="white"/>
            <rect x="200" y="75" stroke="black" id="mesio-occlusal" style="stroke-width: 5px;" width="125" height="150" fill="white"/>

            <polygon stroke="black" id="disto-buccal" style="stroke-width: 5px;" points="0 0 200 0 200 75 75 75" fill="white" />
            <polygon stroke="black" id="mesio-buccal" style="stroke-width: 5px;" points="200 0 400 0 325 75 200 75" fill="white" />

            <polygon stroke="black" id="mesial" style="stroke-width: 5px;" points="400 0 400 300 325 225 325 75" fill="white" />

            <polygon stroke="black" id="mesio-palatal" style="stroke-width: 5px;" points="400 300 200 300 200 225 325 225" fill="white" />
            <polygon stroke="black" id="disto-palatal" style="stroke-width: 5px;" points="200 300 0 300 75 225 200 225" fill="white" />

            <polygon stroke="black" id="distal" style="stroke-width: 5px;" points="0 300 0 0 75 75 75 225" fill="white" />
        </g>
    </svg>
    <select class="color" id="color">  
                  <option value="" >Select Color</option>
                  <option value="black" >Black</option> 
                  <option value="red" >Red</option> 
                  <option value="blue" >Blue</option>
          </select> 

<input type="text " id=""> // This input i want to display the base 64 of an image after converting 


    <button onclick="saveAsBase64()">Save as Base64</button>

选择颜色
黑色
红色
蓝色
//此输入我想在转换后显示图像的基64
另存为Base64

如何在单击按钮后将HTML中的SVG转换为base64,然后在输入字段中显示base64

,但此链接无法解决我的问题。它习惯于描述您自己尝试解决问题所采取的步骤。那会给你一个直截了当的答案。现在使用@DoFlamingo的链接,以及上面的这个链接,您可以添加您的saveAsBase64()-函数吗?函数saveAsBase64(){let str=
数据:image/svg+xml;base64,${window.btoa(新的XMLSerializer().serializeToString(document.getElementById(“svg”)}
;console.log(str);}