我想循環一些鏈接並在href中添加一個數字,所以它會像x.php&ref=333
一樣。將代碼從jquery轉換爲純javascript
<div id="maindiv">
<span><a href="x.php&ref="></a></span>
<span><a href="x.php&ref="></a></span>
<span><a href="x.php&ref="></a></span>
</div>
如何在純JavaScript中編寫此代碼?這裏是jQuery代碼:
$('#maindiv a').each(function(){
var hr = $(this).attr('href');
$(this).attr('href' , hr+333);
})
由於某種原因,我不能使用它,需要編寫純JavaScript代碼。 具體來說,我不知道如何獲得每個a
元素。
可能重複[什麼是通過一組在JavaScript元素的循環最好的方法是什麼?(http://stackoverflow.com/questions/157260/whats-the-best-way-to-loop-通過JavaScript中的元素集) – 2012-02-16 18:47:57