【发布时间】:2017-01-05 17:21:17
【问题描述】:
希望有人可以提供帮助。 我正在尝试动态更改页面上的href,然后将用户指向一个包含以下片段的url:
$(document).ready(function(){
if (document.location.pathname.indexOf('cab2') > -1){
document.getElementsByClassName('resourceLink').setAttribute('href','http://www.myserver.net/cab2/#linkResources');
} else {
document.getElementByClassName('resourceLink').setAttribute('href','http://www.myserver.net/cab/#linkResources');
};
});
在 HTML 中,我使用了几个这样的链接:
<a class="resourceLink" href="" title="Link to Resources section" target="_blank">Resources</a>
我希望脚本会检查访问者用来到达网站的网址,要么
- http://www.myserver.net/cab/ 或者,
http://www.myserver.net/cab2/,然后将相应的 href 设置为:
虽然链接打开的是基本页面(www.myserver.net/cab 或 cab2)而不是 #fragment 页面。
我做错了什么? 感谢您的关注。
【问题讨论】:
-
你正在使用 jQuery,使用它。
标签: javascript html href document-ready