0

I am using the following function:

function pdfChanger(location){ document.getElementById("pdf").setAttribute("data",location); }

which is called here:

<a href="javascript:pdfChanger('documents/SecurityDocs/sample.pdf')">test1</a><br/>

and is designed to change the pdf file displayed in this object:

<object id="pdf" data="documents/sample2.pdf#toolbar=0&navpanes=0&scrollbar=0&page=1&view=FitHtop" 
                    type="application/pdf"></object>

In firefox clicking on the link which evokes this function changes the pdf object correctly. In chrome, however, you have to click on the pdf after you have clicked the link, in order to correctly display the changed pdf. In IE the pdf does not change at all. Why is this happening and what can I do to make this function work uniformly in all browsers?

4

1 回答 1

0

设置属性值并不能保证重新呈现 html 元素。

如果我们改变 img 标签的宽度或高度,我们总是会看到重新渲染的图像。

如果我们改变一个img标签的id,浏览器是否需要重新渲染图像?我不这么认为,但某些浏览器可能会。

从渲染的角度来看,对象标签的“数据”属性可能因浏览器而异。它被视为“数据”或“源 URL”。

为了保证元素的渲染,你最好重写整个元素。您可以创建/添加元素或使用 innerHTML。例如,

document.getElementById('object_div').innerHTML='<object id="pdf" ...></object>'
于 2012-05-16T16:19:28.813 回答