如何在粘贴时自动将URL转换为超链接 [英] How to auto convert an url into a hyperlink when it is pasted
问题描述
当我尝试在http://stackoverflow.com/等文本框中粘贴网址时,它不会自动转换为超链接。
我在这个问题中使用的功能工作正常,但实际上它将取代所有链接,包括标签中的链接(IMG,现有的A HREF)。
i不想使用regx当用户在文本框中粘贴一个网址时,它应自动将其转换为链接
我已尝试使用regx
例如:
what =在任务系统中,有没有办法自动拥有任何网站/网页网址或图片URL会在新窗口中被超链接吗?
所以如果我输入或复制http://www.stackoverflow.com/ 例如在说明中的任何地方,在任何一个内部消息或消息发送给客户端,它自动成为新窗口中的超链接。
http://www.stackoverflow.com /
O如果我在支持描述,内部消息或消息中的任何位置输入图像URL,它会自动成为新窗口中的超链接:
https://static.doubleclick.net/viewad /4327673/1-728x90.jpg
https:// static。 doubleclick.net/viewad/4327673/1-728x90.jpg
这将为我们在任务构建,审阅和创建消息方面节省大量时间。 < br $> b $ b
测试网址
http://www.stackoverflow.com /
http://stackoverflow.com/
https://stackoverflow.com/
www.stackoverflow.com
//stackoverflow.com/
http://stackoverflow.com/ ;
我试过这段代码
函数Linkify(what){
str = what; out =; url =; i = 0;
做{
url = str.match(/((https?:\ / \ /)?([az \ - ] + \。)* [\-\w] +(\。[AZ] {2,4})+(\ / [\w\_\-\?\ = \ & \。] *)*(?![az]))/ i);
if(url!= null){
//获取href值
href = url [0];
if(href.substr(0,7)!=http://)href =http:// + href;
//匹配发生地点
where = str.indexOf(url [0]);
//将它添加到输出中
out + = str.substr(0,where);
//链接它
out + =''+ url [0] +'';
//为下一轮准备str
str = str.substr((其中+ url [0] .length));
}否则{
out + = str;
str =;
}
} while(str.length> 0);
退出;
}
小提琴不起作用
当我们在文本框中粘贴网址时,我们可以自动转换它吗在堆栈中我可以有一些例子吗?
谢谢。
br>
When I tried to paste an url in the text box like http://stackoverflow.com/ it doesn't convert to a hyperlink automatically.
The function that i use in this question works fine, but actually it will replace all links including links in tags (IMG, existing A HREFs).
i dont want to use regx when a user paste's a url in a text box it should automatically convert that to link
i've tried this using regx
For example:
what = "In the task system, is there a way to automatically have any site / page URL or image URL be hyperlinked in a new window?
So If I type or copy http://www.stackoverflow.com/ for example anywhere in the description, in any of the internal messages or messages to clients, it automatically is a hyperlink in a new window.
http://www.stackoverflow.com/
Or if I input an image URL anywhere in support description, internal messages or messages to cleints, it automatically is a hyperlink in a new window:
https://static.doubleclick.net/viewad/4327673/1-728x90.jpg
https://static.doubleclick.net/viewad/4327673/1-728x90.jpg
This would save us a lot time in task building, reviewing and creating messages.
Test URL's
http://www.stackoverflow.com/
http://stackoverflow.com/
https://stackoverflow.com/
www.stackoverflow.com
//stackoverflow.com/
http://stackoverflow.com/";
I've tried this code
function Linkify(what) {
str = what; out = ""; url = ""; i = 0;
do {
url = str.match(/((https?:\/\/)?([a-z\-]+\.)*[\-\w]+(\.[a-z]{2,4})+(\/[\w\_\-\?\=\&\.]*)*(?![a-z]))/i);
if(url!=null) {
// get href value
href = url[0];
if(href.substr(0,7)!="http://") href = "http://"+href;
// where the match occured
where = str.indexOf(url[0]);
// add it to the output
out += str.substr(0,where);
// link it
out += ''+url[0]+'';
// prepare str for next round
str = str.substr((where+url[0].length));
} else {
out += str;
str = "";
}
} while(str.length>0);
return out;
}
fiddle that's not working
Is it possible to convert it automatically when we paste a url in a text box like we are getting in stack over flow can I have some examples?
Thanks.
推荐答案
最后我做到了。您可以使用jQuery插件 linkify [ ^ ]为此。
演示
看看它如何正在工作。
[演示]在粘贴一些文本时将网址转换为链接TextArea [ ^ ]
Finally I did it. You can use jQuery Plugin linkify[^] for this.
Demo
See how it is working.
[Demo] Convert Urls to Link while pasting some Text in a TextArea [^]
>
这篇关于如何在粘贴时自动将URL转换为超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!