我有一个用户选择的pdf语句显示在同一网页的框架中。首先,我想要显示可点击的pdf列表,然后当用户选择保留网页其余部分的同一页面时,将加载pdf文件信息框。
我正在使用.js文件中的全局变量(userSelection)存储用户点击的内容(请参阅函数getUserSelection())。在setSource()中访问相同的变量。这是在加载iframe时调用的。
现在,在firefox上,函数setSource()永远不会被调用,并且页面似乎没有响应鼠标单击。我究竟做错了什么?此功能非常基本,因此必须有一些解决方法。请帮忙!! --jody
//Javascript
var userSelection;
function getUserSelection(yearandDoc)
{
userSelection="0";
switch (yearAndDoc)
{
case '0506BS': userSelection="1";
break;
case '0506RP': userSelection="2";
break;
default: break;
}
}
function setSource ()
{
switch (userSelection){
case "1": document.getElementById("stmtFrame").src = "2005-06_BS_IE_Stmt.pdf";
userSelection="0";
break;
case "2": document.getElementById("stmtFrame").src = "2005-06_RP.pdf";
userSelection="0";
break;
default: document.getElementById("stmtFrame").src = "";
break;
}
}
<!-- Here is the HTML -->
<div>
<p>2005/06 : <a href="javascript:getUserSelection('0506BS')">Balance Sheet</a> ||
<a href="javascript:getUserSelection('0506RP')">Receipts & Payments</a></p>
<iframe id="stmtFrame" onload="javascript:setSource()" frameborder="0" width="100%" height="700" </iframe>
</div>
答案 0 :(得分:0)
首先,有一些错误:
function getUserSelection(yearAndDoc) // not yearandDoc
<iframe ... ></iframe> // before you were missing >
其次,我认为更改iframe的src属性会调用onload事件。这意味着您的代码是一个无限循环,因为onload事件会触发一个调整src属性的函数。
如果我理解你要做什么,你应该能够合并这两个函数,这样只有当你点击其中一个链接时才会改变src属性:
function getUserSelection(yearAndDoc)
{
switch (yearAndDoc)
{
case '0506BS': document.getElementById("stmtFrame").src = "2005-06_BS_IE_Stmt.pdf";
break;
case '0506RP': document.getElementById("stmtFrame").src = "2005-06_RP.pdf";
break;
default: break;
}
}
HTML:
<div>
<p>2005/06 : <a href="#" onclick="getUserSelection('0506BS')">Balance Sheet</a> ||
<a href="#" onclick="getUserSelection('0506RP')">Receipts & Payments</a></p>
<iframe id="stmtFrame" frameborder="0" width="100%" height="700"></iframe>
</div>