链接href中的Javascript动态更改

时间:2012-11-16 05:26:03

标签: java jquery search hyperlink href

根据用户提交的信息,是否有明确的方法来更改href中url的内容?

例如:用户在搜索框中提交“篮球”一词,结果如下:

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

如果用户提交了“足球”,则链接将变为:

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

2 个答案:

答案 0 :(得分:2)

你错过了href的近距离报价,你可以这样做。

<强> Live Demo

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

答案 1 :(得分: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);