Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用URL-s JavaScript替换文本链接_Javascript - Fatal编程技术网

用URL-s JavaScript替换文本链接

用URL-s JavaScript替换文本链接,javascript,Javascript,我有一个订单提交表单,我想用相应的HTML链接替换URL文本。我在Stackoverflow上找到了以下代码: get_url = function() { var urls = document.getElementById('w_descr').firstChild; urls.nodeValue = replaceURLWithHTMLLinks(urls.nodeValue); } function replaceURLWithHTMLLinks(text) { var e

我有一个订单提交表单,我想用相应的HTML链接替换URL文本。我在Stackoverflow上找到了以下代码:

get_url = function() {
  var urls = document.getElementById('w_descr').firstChild;
  urls.nodeValue = replaceURLWithHTMLLinks(urls.nodeValue);
}

function replaceURLWithHTMLLinks(text) {
  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  return text.replace(exp,"<a href='$1'>$1</a>"); 
}
get_url=function(){
var url=document.getElementById('w_descr').firstChild;
url.nodeValue=replaceurlwithhtmlinks(url.nodeValue);
}
函数replaceurlwithhtmlinks(文本){
var exp=/(\b(https?| ftp |文件):\/\/[-A-Z0-9+&@#\/%?=~~|!:,.;]*[-A-Z0-9+&@#\/%=~|]/ig;
返回文本。替换(exp,“”);
}
我在点击表单提交按钮时调用get_url()函数。它很好用。但是,提交的订单具有编辑功能。如果编辑订单并再次单击“提交”按钮,该功能将再次工作并复制现有链接

有谁能帮我想一想,怎样才能防止这种情况发生?我的意思是-如何修改上面的脚本,以避免复制已经以HTML形式存在的链接


提前感谢。

始终存储文本“普通”(无链接),并且仅在输出文本以供显示时添加链接


当输出文本进行编辑时,请输出“纯”文本。

目前没有办法做到这一点。数据存储在数据库中,如果我对document.load上的所有文本调用该函数,它将完成工作-将文本替换为HTML链接,但链接仍然看起来是HTML代码,而不是实际的链接。有没有一种方法可以检查链接是否被链接包围,而不调用该函数?我的答案是,您应该将数据以纯文本形式存储在数据库中(没有链接)。添加文档上的链接并非天生不可能。加载:。当然,你也可以在服务器端完成,这对我来说似乎更整洁。