【问题标题】:truncate links with jQuery使用 jQuery 截断链接
【发布时间】:2012-02-08 14:56:23
【问题描述】:

我在这里的第一篇文章,但通过潜伏我已经学到了很多 =) 这一次,我找不到问题的解决方案,尽管它看起来很容易实现。

我有一个博客文章的链接列表,由 Feed2JS 生成。不幸的是,作为此源的 RSS 提要向我不想要的链接添加了锚点。我无法更改提要,因为它是在 RapidWeaver 中自动生成的。

是否可以使用 jQuery 从 url 中的哈希中删除所有内容?例如:改变

http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php#unique-entry-id-31

http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php

我对 jQuery 很陌生,还有很多东西要学,所以请保持简单的回答。

杰伦

【问题讨论】:

    标签: jquery


    【解决方案1】:

    如果你只是想截断网址,你可以这样做

    var url = "http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php#unique-entry-id-31";
    var index = url.indexOf('#') != -1 ? url.indexOf('#') : url.length
    alert(url.substring(0,index));
    

    输出:

    http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php
    

    示例:http://jsfiddle.net/2dXXx/

    【讨论】:

      【解决方案2】:

      您不需要 jQuery 来执行此操作,因为 javascript 通过 window.location 支持它。

      看看这个答案,了解你想要做什么javascript window location href without hash?

      【讨论】:

        【解决方案3】:

        我喜欢这样做:

        var a = document.createElement("a");
        a.href = "http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php#unique-entry-id-31";
        alert(a.protocol + "://" +  a.host + a.pathname + a.search);
        

        http://jsfiddle.net/karim79/7pMbk/1

        【讨论】:

        • 去掉用户名和密码
        【解决方案4】:

        当然你可以这样做:

        var str = 'http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php#unique-entry-id-31';
        var substr = str.split('#');
        // substr[0] contains "http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php"
        // substr[1] contains "unique-entry-id-31"
        

        请注意,这是来自 JavaScript API。

        【讨论】:

        • 谢谢大家,非常感谢!
        【解决方案5】:

        你不需要为此使用 jQuery:

        function truncate(href) {
          var a = document.createElement('a');
          a.setAttribute('href', href);
        
          a.hash = '';
          return a.href;
        }
        

        【讨论】:

          【解决方案6】:

          不需要对 jQuery 的依赖,您可以使用正则表达式,如 jsFiddle 所示

          var url = "http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php#unique-entry-id-31"
          console.log("base url >> " + url)    
          var matcher = /(.*)#.*/.exec(url)
          var truncated = matcher[1]
          console.log("truncated url >> " + truncated)    
          

          【讨论】:

            【解决方案7】:
            var url = "http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.phpunique-entry-id-31";
            
            alert(url.substring(0,url.indexOf('#'))); // will give you empty string if you dont have # in the url.
            

            所以,你可以使用

            alert(url.split('#')[0]);
            

            【讨论】:

              猜你喜欢
              • 2012-06-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-02-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多