试图动态更改锚HREF

时间:2014-07-08 16:29:31

标签: javascript html anchor

我有一个网站,其中包含许多内部报告,这些报告每周都会从各种来源更新,每年我们都会归档年终版本。所以我们在服务器上有一个文件夹结构,如下所示:

Department
  Current
     report1
     report2
     <etc.>
  2013
     report1
     report2
     <etc.>
  2012
  <etc.>

我创建了一个选择,让用户可以选择所需的时间段:

<select id="period" onchange="UpdateLinks(this)">
  <option value="current" selected>Current</option>
  <option value="2013">2013</option>
  <option value="2012">2012</option>
  <option value="2011">2011</option>
  <option value="2010">2010</option>
</select>

实际链接定义为:

<a onclick="EnablePrint();" href="Offshore\Current\NYK_Offshore_Summary.htm" 
id="offshoreSales"  TARGET="report">Sales</a><br>

请注意,该网站分为两个框架 - 这些项目显示在“菜单”一侧,报告显示在“报告”一侧,如果相关的话。

在UpdateLinks()中,我尝试更新锚点中的hrefs以指向相应的文件夹。根据这里的一些条目,这应该工作,但它不适合我!:

function UpdateLinks(selected) {
    var myURL = 'Offshore\\' + selected.value + '\\';
    alert('New URL prefix: ' + myURL);
    var newURL = myURL + 'NYK_Offshore_Summary.htm';
    document.getElementByID('offshoreSales').href = newUrl;
    newURL = myURL + 'NYK_Offshore_Assets.htm';
    document.getElementByID('offshoreAssets').href = newURL;
}

警报()正在发生,我看到了正确的值 - 例如“当前”,或“2013”​​,但行

document.getElementByID('offshoreSales')。href = newUrl;

不是。我错过了什么?

1 个答案:

答案 0 :(得分:1)

错误输入:

document.getElementByID('offshoreSales').href = newUrl;

尝试:

document.getElementById('offshoreSales').href = newUrl;
相关问题