HTML& GRAILS-网址文本不显示为超链接 [英] HTML&GRAILS - urls text is not displayed as hyperlink

查看:254
本文介绍了HTML& GRAILS-网址文本不显示为超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下gsp代码:

<p class="common-text ">${direction?.description?.toHtml()?.decodeHTML()}</p>

direction?.description是用户在某些输入字段中输入的文本.该文本可能包含网站网址,例如Visit my new site www.example.com

direction?.description is a text which the user entered in some input field. This text could includes website URLs like Visit my new site www.example.com

如何验证某些文本中是否包含URL,并显示为超链接?

How can I verified if some of the text contains a URL, and if so display it as hyperlink?

上面的文本应转换为以下内容:

The text above should be transformed to something like:

Visit my new site <a href="www.example.com"> www.example.com </a>

推荐答案

您有两种方法可以做到这一点.

You have two way to do this.

您可以创建一个自定义Grails标签库在您的文本中查找网址,并在需要时用<a>标记替换.可以这样使用:

You could create a custom Grails taglib that look for URLs in your text and replace with <a> tags when needed. It could be used like this:

<myTagLib:replaceUrls>${direction?.description}</myTagLib:replaceUrls>

使用javascript的客户端

您可以使用JavaScript库搜索字符串中的URL,然后将其自动替换为适当的超链接.有关此技术的更多信息,请参见此问题.

这篇关于HTML&amp; GRAILS-网址文本不显示为超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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