【问题标题】:Making link to go somewhere else than its default href使链接转到默认href以外的其他地方
【发布时间】:2011-12-29 17:35:19
【问题描述】:

我想获取链接文本并将其附加到 URL 并打开添加了查询字符串的新 URL Onclick of the Original Link..如何使用 javascript 或 jquery 获取链接文本?

<a href="www.mysite.com/search.aspx?kwd=" onClick="location.href='http://mysite.com/search.aspx?kwd='+ Grab text 'kangaroo' and append here as QueryString>Kangaroo</a>

【问题讨论】:

    标签: javascript jquery hyperlink query-string


    【解决方案1】:

    您可以通过this 访问当前锚点。然后可以通过this.innerHTML获取文本。

    这样的……

    <a href="www.mysite.com/search.aspx?kwd=" onClick="location.href='http://mysite.com/search.aspx?kwd='+ this.innerHTML; return false;">Kangaroo</a>
    

    【讨论】:

    • 它工作得很好,但我遇到了一个问题,链接文本像两个以上的单词,比如“心脏健康”,所以当它被添加时,它必须看起来像 mysite.com/search.aspx?kwd=Heart%20Health..How 你是否在广告中显示了 %20in 之间?也没有空格,要修剪吗?
    • @Gasim encodeURIComponent 已经设置为进行必要的替换,也可以尝试使用 MDN 文档,w3schools 较差:developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
    • 是的,我刚刚意识到。因此,将其删除以免混淆他 :) 谢谢您提供的信息。我总是用谷歌搜索一些东西,首先出现的总是 w3schools。现在要使用 MDN
    • @T. Stone 你如何修剪 this.innerHTML 返回的内容以去除空格?
    【解决方案2】:
    $('.your-url').click(function(event) {
        event.preventDefault();
        window.location= $(this).attr('href') + encodeURIComponent($(this).text());
    });
    

    我注意到没有其他答案将链接中的文本编码为查询字符串参数。

    内联(如您的示例)将如下所示:

    <a href="www.mysite.com/search.aspx?kwd=" onClick="location.href = this.href + encodeURIComponent($.trim($(this).text()))">Kangaroo</a>
    

    return false 应该是不必要的,因为一旦您更改了location 对象,脚本就会停止运行并且页面会更改。

    更新

    您可以使用$.trim() 来:

    删除字符串开头和结尾的空格。

    来源:http://api.jquery.com/jquery.trim/

    【讨论】:

    • kwd=%26nbsp%3BHeart%20Health...我需要摆脱空间??我该怎么做?
    • @AnjuThapa jQuery 有 $.trim() 方法来删​​除多余的空白。我更新了我的答案。
    【解决方案3】:
    $('a.your-url').click(function(e) {
      e.preventDefault();
      url = $(this).attr('href') + $(this).text();
      location.href = url;
    
    });
    

    【讨论】:

      【解决方案4】:

      点击时要将页面发送到链接的href + text,这应该可以:

      $("a").click(function(){
           location.href = $(this).attr("href") + $(this).text();
           return false;
      });
      

      但是为什么不在页面加载时正确设置href,并完全摆脱所有这些onclick 处理程序呢?

      $("a").each(function(i, el) {
           var $el = $(el);
           $el.attr("href", $el.attr("href") + encodeURI($el.text()));
      });
      

      【讨论】:

      • 我想你可能误解了他的问题。我相信他希望他的最终点击链接看起来像:location.href='http://mysite.com/search.aspx?kwd=kangaroo'
      • 怎么抢袋鼠价值?那是我的问题。我不能只添加到 url,因为它是动态的
      • innerHTML 在纯 JS 中。 jquery中的$.text()
      • @AnjuThapa - 我的第一个答案是重大失败 - 请参阅我的编辑。我很抱歉。
      • @AnjuThapa - 很高兴听到这个消息。祝你好运!
      【解决方案5】:

      jQuery 示例:

      $('a.link').click(function () {
          var $this = $(this),
              href = $this.attr('href');
      
          window.location = href + encodeURIComponent($this.text());
          event.preventDefault();
          return false;
      });
      

      Demo

      【讨论】:

      • 哎呀。我们同时写的。我总是把 e.preventDefault() 放在其他任何东西之前。你把它放在其他一切之后。有什么区别?
      • 不多,只要在函数体中调用就行了
      • @Gasim 在 jQuery 事件处理程序中,return false;event.preventDefault(); event.stopPropagation(); 相同。不,在事件处理程序中何时调用 event.preventDefault() 并没有区别。
      • cat=%26nbsp%3BHeart%20Health 我如何摆脱 %26nbsp%3B 这是我认为的空间?
      • @AnjuThapa,你得到这个是因为你使用的是 innerHtml 而不是我的例子
      猜你喜欢
      • 1970-01-01
      • 2015-12-07
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      相关资源
      最近更新 更多