3

我写了这个,所以它用类名改变了锚的href,但我也希望它改变目标,我该如何添加这个?

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;
        }      
    }
}
4

2 回答 2

7
change[i].setAttribute('target', '_blank');
于 2013-03-12T14:44:00.633 回答
0

尝试这个

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.com";
     }      
  }
}
于 2013-03-12T15:03:20.570 回答