jQuery - 识别并标记textarea中的链接 [英] jQuery - Identify and mark link in textarea

查看:128
本文介绍了jQuery - 识别并标记textarea中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个textarea

I have a textarea

<textarea id="postContent" class="input-block-level" placeholder="Tell us about it!" rows="4"></textarea>

我需要一个突出显示textarea内部链接的脚本,将它们存储在一个变量中(数组) )供以后处理并删除所有html标签。这种行为在Twitter上的推文框中是众所周知的。

and I need a script that highlights the links written inside the textarea, stores them in a variable (array) for later processing and removes all html-tags. The behavior is well-know from the tweet-box on twitter.

所以内容可能是


看看我今天在www.stackoverflow.com上找到的内容!

Look what I have found today on www.stackoverflow.com!

在这种情况下,应突出显示www.stackoverflow.com(包装在< p> fe)没有!。

and in that case, www.stackoverflow.com should be highlighted (wrapped in a <p> f.e.) without the "!".

你会如何用jQuery做到这一点?

How would you do that with jQuery?

推荐答案

如果你想突出显示链接,那么你必须使用textarea的innerHTML ....(我不建议那样做你的正则表达式在这里= var regEx = /((ht | f)tps?:// \ S *)/ g;

If you want to highlight link then you have to work with innerHTML of textarea....(I would not suggest that) your regex goes here = var regEx = /((ht|f)tps?://\S*)/g;

其次,你可以识别链接在textarea中使用click事件... click handler应该查看它是url还是纯文本..如果链接打开新窗口..返回false ....

Secondly, you can identify links using click event inside textarea...click handler should look whether it is a url or plain text..if a link open new window..else return false....

这篇关于jQuery - 识别并标记textarea中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆