根据用户提交的信息,是否有明确的方法来更改href中url的内容?
例如:用户在搜索框中提交“篮球”一词,结果如下:
<a href="http://google.com/basketball>More information</a>
如果用户提交了“足球”,则链接将变为:
<a href="http://google.com/football>More information</a>
答案 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);