gpt4 book ai didi

JavaScript 函数切换函数不起作用

转载 作者:行者123 更新时间:2023-11-28 05:39:23 25 4
gpt4 key购买 nike

我最近发布了一个问题并建议将此作为答案,但它不起作用。为什么 ?

<div class="text-center">           
<embed src="https://www.setasign.com/files/demo-files/pdfs/lenstown/Fact-Sheet.pdf" width="300" height="300" type='application/pdf' id="thePdf">
</div>

<div class="button">
<button class="btn-info" type="button" onclick="switchPdf();">Search</button>
</div>

<script>
function switchPdf() {
var fileA = 'https://www.setasign.com/files/demo-files/pdfs/lenstown/Fact-Sheet.pdf',
fileB = 'https://www.setasign.com/files/demo-files/pdfs/tektown/Fact-Sheet.pdf',
elem = document.getElementById('thePdf');

elem.src = elem.src == fileA ? fileB : fileA;
}
</script>

基本上有一个默认的 PDF 会在页面打开时加载,一旦我单击按钮,我希望 pdf 更改为另一个 pdf。但它不起作用

最佳答案

更改为src属性 <embed> , <object>等等...
不受影响

必须替换为新元素

function switchPdf() {
var fileA = 'https://www.setasign.com/files/demo-files/pdfs/lenstown/Fact-Sheet.pdf',
fileB = 'https://www.setasign.com/files/demo-files/pdfs/tektown/Fact-Sheet.pdf',
container = document.getElementById('thePdfContainer'),
elem = document.getElementById('thePdf');

var newFile = (elem.src === fileA) ? fileB : fileA;

container.innerHTML = '<embed src="' + newFile + '" width="300" height="300" type="application/pdf" id="thePdf">';

console.log(container.innerHTML);
}
<div id="thePdfContainer" class="text-center">           
<embed src="https://www.setasign.com/files/demo-files/pdfs/lenstown/Fact-Sheet.pdf" width="300" height="300" type='application/pdf' id="thePdf">
</div>

<div class="button">
<button class="btn-info" type="button" onclick="switchPdf();">Search</button>
</div>

关于JavaScript 函数切换函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39057191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com