【问题标题】:Javascript dynamic change in link href链接href中的Javascript动态变化
【发布时间】:2012-11-04 20:19:51
【问题描述】:

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

例如:用户将“篮球”一词提交到搜索框中,结果是以下链接。

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

如果用户提交让我们说“足球”,则链接将变为:

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

【问题讨论】:

标签: java jquery search hyperlink href


【解决方案1】:

你缺少href的右引号,你可以这样做。

Live Demo

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

【讨论】:

    【解决方案2】:

    给定:<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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-01
      • 2013-12-13
      • 2016-11-10
      • 2016-07-26
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多