1
我最近发布了一个问题,并建议将其作为一个答案,但它不工作。为什么?JavaScript函数切换功能不工作
<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。但它不工作
检查'elem.src'的值并将其与其他变量进行比较......我怀疑你会得到相同的值... – Rayon
“不工作”是什么意思?代码应该很好地切换src属性,但由于跨源策略,内容可能不会被加载。您在开发控制台中是否有任何错误消息? – Szab
[JavaScript:更改嵌入标签的src-attribute]的可能重复(http://stackoverflow.com/questions/2493706/javascript-changing-src-attribute-of-a-embed-tag) – WhiteHat