admin管理员组文章数量:1356032
I've wrote this so it changes the href of the anchor with the class name but I also would like it to change the target, how do I add this?
window.onload = function() {
var change = document.getElementsByTagName('a');
for (var i = 0; i < change.length; i++) {
if (change[i].className.match('(^|\\s+)classnamegoeshere(\\s+|$)')) {
change[i].href = "/urlgoeshere";
break;
}
}
}
I've wrote this so it changes the href of the anchor with the class name but I also would like it to change the target, how do I add this?
window.onload = function() {
var change = document.getElementsByTagName('a');
for (var i = 0; i < change.length; i++) {
if (change[i].className.match('(^|\\s+)classnamegoeshere(\\s+|$)')) {
change[i].href = "/urlgoeshere";
break;
}
}
}
Share
Improve this question
asked Mar 12, 2013 at 14:42
user1975031user1975031
2611 gold badge3 silver badges9 bronze badges
4
- You should take a look at jQuery, which would make this task simple and cross browser. – Mikael Östberg Commented Mar 12, 2013 at 14:44
- 3 @MikaelÖstberg I know jQuery and could've done this in a breeze although for this situation, I didn't see the need in including jQuery for this one thing. – user1975031 Commented Mar 12, 2013 at 14:45
- Ok, your call. Maybe jQuery isn't the silver bullet one off solution to all worlds problem. I just wanted to make you aware of its existence as you appear to be a pretty new user here. – Mikael Östberg Commented Mar 12, 2013 at 14:49
- Yeah, I love jQuery, I just didn't need it in this instance. – user1975031 Commented Mar 12, 2013 at 14:53
2 Answers
Reset to default 7change[i].setAttribute('target', '_blank');
Try this
window.onload = function() {
var change = document.getElementsByTagName('a');
for (var i = 0, j =change.length; i < j ; i++) {
if ((' ' + change[i].className + ' ').indexOf(' ' + "smeCls" + ' ') > -1) {
change[i].href = "http://www.google.";
}
}
}
本文标签: javascriptChange href and target of an anchorStack Overflow
版权声明:本文标题:javascript - Change href and target of an anchor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744058610a2583708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论