我已成功使链接可点击,但如何使它们再次恢复为不可点击状态?
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();
});
最佳答案
检查这个 fiddle :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);
}
关于javascript - 切换链接以使其可点击/不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8803470/