2012-05-29 22 views

回答

0

使用the .attr() function改变一个元素的属性:

$('#mylink').attr('href', 'newvalue'); 

而且,只是一个供参考(这甚至可能是你为什么要改变它),href="www.link.com"将导致断开的链接,除非有实际上是一个名为www.link.com的文件,其位于与托管链接的页面相同的路径中。要转到另一个域,URL需要完全限定以包含协议:href="http://www.link.com"

+0

我们目前有一个php函数用于替换html内容,但函数的定义如下:replaceContent($ selector,$ content)。所以现在我正在这样做:replaceContent('#mylink [href]'),'http://www.link.com')。这似乎也取代了链接的文字,这是不可取的。有任何想法吗? – user1074861

+0

@ user1074861:如果不知道该函数的代码或者它的作用,我无法真正地建议您使用PHP函数。最初的问题是关于使用jQuery更改元素属性,这非常简单。你在PHP中做什么是完全不同的。 – David

0

jQuery's .attr() method将是执行此操作的最佳方式。它像一个魅力。以下是您想要执行的代码:

$('#mylink').attr('href', 'http://www.newlink.com'); 

此外,请确保您的href包含协议。 (http://,https://等)

0

使用jQuery .attr()可以轻松实现。

var link = $("#mylink"); //the target anchor 
link.attr("href","{your url here}");