2012-11-16 76 views
-2

是否有一个明确的方法来改变基于用户提交的信息的href中的url内容?Javascript动态变化链接href

例如:用户在搜索框中输入单词“篮球”,结果为以下链接。

<a href="http://google.com/basketball>More information</a> 

如果提交的用户可以说“足球”的链接,然后将改变:

<a href="http://google.com/football>More information</a> 
+0

,因为它的重复 [如何更改为HREF使用jQuery的超链接(http://stackoverflow.com/问题/ 179713/how-to-change-the-href-for-a-hyperlink-using-jquery) – nu6A

+0

你可以在这里看到答案......这是一个重复的问题.... [http://stackoverflow.com /问题/ 179713](http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery) – Ofear

回答

2

你缺少HREF接近报价,你可以这样来做。

​​

var url = $('#aId').attr('href'); 
firstPart = url.substring(0, url.lastIndexOf('\/')+1); 
$('#aId').attr('href', firstPart + "Football"); 
1

考虑:<a id="url" href="http://google.com/basketball">More information</a> <input id="sport" type="text" val="soccer" />

var $val = $('#sport').val() 
$("#url").attr("href", "http://www.google.com/" + $val);