更改appendChild函数

问题描述 投票:0回答:1

所以我有这个脚本将b64字符串解码为pdf

// JavaScript Document
function decode() {
    // The Base64 string of a simple PDF file
    var b64decode = BASE64 STRING

    // Decode Base64 to binary and show some information about the PDF file (note that I skipped all checks)
    var bin = atob(b64decode);


    // Embed the PDF into the HTML page and show it to the user
    var obj = document.createElement('object');
    obj.style.width = '100%';
    obj.style.height = '842pt';
    obj.type = 'application/pdf';
    obj.data = 'data:application/pdf;base64,' + b64decode;
    document.body.appendChild(obj);

    // Insert a link that allows the user to download the PDF file
    var link = document.createElement('a');
    link.innerHTML = 'Download PDF file';
    link.download = 'file.pdf';
    link.href = 'data:application/octet-stream;base64,' + b64decode;
    document.body.appendChild(link);
}

并且脚本在页面底部创建一个对象。相反,我需要脚本在一个空的div容器内创建一个对象。

您可能会猜到,我不是JS专家(也不是业余专家)

谢谢!

javascript object pdf dom appendchild
1个回答
0
投票
您的代码的这一行执行附加操作:

document.body.appendChild(link);

您必须将其更改为div。为此,您需要给您的div一个ID:

<div id="pdf-div"></div>

现在您可以将appendChild指令替换为:

document.getElementById('pdf-div').innerHTML = link;

© www.soinside.com 2019 - 2024. All rights reserved.