[英]JavaScript Function Switch Function Not Working
I recently posted a question and suggested this as an answer but it is not working. 我最近发布了一个问题并建议将其作为答案,但它无效。 Why ? 为什么?
<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>
Basically there is a default PDF which loads on page open, and once I click the button I want the pdf to change to the other pdf. 基本上有一个默认的PDF在页面打开时加载,一旦我点击按钮我想要pdf更改为另一个pdf。 But it is not working 但它没有用
changes to src
attribute on <embed>
, <object>
, etc... 更改<embed>
, <object>
等上的src
属性...
do not take affect 不要生效
have to replace with a new element 必须用新元素替换
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.