【问题标题】:Toggle link to make the clickable/unclickable切换链接以使可点击/不可点击
【发布时间】:2016-01-02 00:05:13
【问题描述】:

我已成功使链接可点击,但如何让它们再次恢复到不可点击状态?

HTML

<div id="links">
    http://google.com <br>
    http://facebook.com <br>
    http://youtube.com
</div>
<button>Toggle!</button>

JavaScript

$.fn.replaceUrl = function() {  
    var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
    this.each(function() {
        $(this).html(
            $(this).html().replace(regexp,'<a href="$1">$1</a>')
        );
    });
    return $(this);
}

$('button').click(function(){
    $('div').replaceUrl();
});

http://jsfiddle.net/6Zvs6/

【问题讨论】:

  • 小提琴是添加到问题中的好东西,但也请将代码发布到问题中。这样人们可以在 jsFiddle 离线时帮助您,并且这个问题将来也会有用。

标签: javascript jquery toggle


【解决方案1】:

检查这个小提琴:http://jsfiddle.net/2ttWS/

代码:

$.fn.replaceUrl = function() {
    if($(this).find('a').length > 0) {
        $(this).find('a').each(function() {
           $(this).replaceWith($(this).text());
        });
    }
    else {
        var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
        this.each(function() {
            $(this).html(
            $(this).html().replace(regexp, '<a href="$1">$1</a>'));
        });
    }
    return $(this);
}

【讨论】:

    【解决方案2】:

    你可以用这个:

    $.fn.removeHyperlink = function() {  
        var regexp = /<\/?a.*?>/gi;
        this.each(function() {
            $(this).html(
                $(this).html().replace(regexp,'')
            );
        });
        linksShown = false;
        return $(this);
    }
    

    这里是working example。 这不是高级的,因此它可以破坏一些复杂的链接,但应该适用于所有正常情况。

    【讨论】:

      【解决方案3】:

      试试这个

      $.fn.replaceUrl = function() {  
          var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
          this.each(function()
          {
              if(($(this).html()).indexOf("href")!= -1)
              {
                      var tx = ($(this).html()).split("</a>");
                      var tmp="";
                      for(var i=0;i<tx.length-1;i++)
                      {
                        if((tx[i].indexOf("href")!=-1)&&(tx[i].indexOf("<br>")==-1))
                            tmp+=tx[i].split(">")[1]+" <br>";
                  else
                      tmp+=tx[i].split(">")[2]+" <br>";
                      }
      
                 $(this).html(tmp)
              }
              else
              {
              $(this).html(
                             $(this).html().replace(regexp,'<a href="$1">$1</a>')
      
              );
              }
          });
          return $(this);
      }
      
      $('button').click(function(){
          $('div').replaceUrl();
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-02
        • 1970-01-01
        • 1970-01-01
        • 2011-04-10
        • 2014-01-22
        • 1970-01-01
        • 1970-01-01
        • 2012-12-26
        相关资源
        最近更新 更多