我想更改頁面上的所有鏈接,它適用於沒有類的鏈接,但不適用於具有類的鏈接。更改頁面上的所有鏈接
這是我使用的代碼:
window.onload = function() {
/* onload code */
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
anchors[i].href = "http://www.example.com/?redirect=" + anchors[i].href
}
}
它適用於鏈接,如:
<a id="box-left" href="http://www.google.com"></a>
但是,這並不工作:
<a class="links" href="redirect.php?link=125411" onclick="launch();" target="_blank"></a>
嘗試使用第一錨[I] .removeAttribute( 「HREF」);然後設置它。 而不是window.onload使用document.onload可以被觸發。 如果您將代碼提供給jsfiddle或jsbin,會更好。 – Givi 2013-04-08 17:13:30