Переключить ссылку, чтобы сделать кликабельным / не кликабельным - PullRequest
0 голосов
/ 10 января 2012

Я успешно сделал ссылки кликабельными, но как мне сделать так, чтобы они снова вернулись в их неактивное состояние?

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/

Ответы [ 3 ]

3 голосов
/ 10 января 2012

Проверьте эту скрипку: 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);
}
0 голосов
/ 10 января 2012

Попробуйте это

$.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();
});
0 голосов
/ 10 января 2012

Вы можете использовать это:

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

Вот рабочий пример .Это не продвинутое, поэтому может сломать некоторые сложные ссылки, но должно работать со всеми обычными случаями.

...