2012-10-26 42 views

回答

3

要改变一个链接,

<a href="http://www.google.com" id="link">One Link</a> 

$('#link')[0].href = '#'; 

普通的JavaScript:

document.getElementById('link').href = '#' 

更改多个链接,

<a href="http://www.google.com" class="links">One Link</a> 
<a href="http://www.yahoo.com" class="links">One Link</a> 
<a href="http://www.hotmail.com" class="links">One Link</a> 


$('a.links').each(function() { 
    this.href = '#'; 
}); 
+0

@ user1658756尝试'的document.getElementById( '链接' ).href = '#';'和看到更新后 –

1
<!-- HTML --> 
<a href="http://stackoverflow.com/" class="mylink">Link</a> 

// JavaScript 
$("a.mylink").prop("href", "#"); 
+0

'.attr()'抄用于'href'。从[jQuery 1.6.1发行说明](http://blog.jquery.com/2011/05/12/jquery-1-6-1-released/):*应使用.prop()方法对于布尔属性/属性和html中不存在的属性(如window.location)。*“ – Eric

+0

@Eric从技术角度来看,'$(”#smth“)。prop(”href“,” #“)'在这个上下文中与'document.getElementById(”smth“)。href =”#“'相同,它比正常的快。我看到它没有问题。 – VisioN