---
title: "在文本框里添加超链接"
author: "wdbk123"
date: "2010-05-31T10:16:41Z"
tags: ["ActionScript", "CSS", "HTML"]
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2010-05-31T10:16:41Z"
id: 679670
url: "https://wdbk123.iteye.com/blog/679670"
---

**问题**  
我想在文本框里加入超链接  
**解决办法**  
利用HTML &lt;a href&gt; 标签设置文本框的htmlText 属性，也可用TextFormat 对象的url属性  
**讨论**  
两种方法都有个前提那就是文本框的html 属性必须先设为TRue：

+展开

-ActionScript

field.html = true;

在文本框的htmlText 属性里设置HTML超链接标签&lt;a href&gt;：

+展开

-ActionScript

field.htmlText = "&lt;a href='http://www.rightactionscript.com'&gt;Website&lt;/a&gt;";

还可指定目标窗口的打开方式：

+展开

-ActionScript

field.htmlText = "&lt;a href='http://www.rightactionscript.com' target='blank'&gt;Website&lt;/a&gt;";

当鼠标移到超链接上时鼠标图标会变成手型图标，下面给超链接加上下划线和颜色：

+展开

-ActionScript

var htmlLink:String = "&lt;font color='#0000FF'&gt;&lt;u&gt;";  
htmlLink += "&lt;a href='http://www.rightactionscript.com'&gt;Website&lt;/a&gt;";  
htmlLink += "&lt;/u&gt;&lt;/font&gt;";  
field.htmlText = htmlLink;

另外用TextFormat对象的url属性也可达到HTML标签同样的效果：

+展开

-ActionScript

field.text = "Website";  
var formatter:TextFormat = new TextFormat( );  
formatter.url = "http://www.rightactionscript.com/";  
field.setTextFormat(formatter);

通过TextFormat 对象的target 属性设置链接打开窗口方式：

+展开

-ActionScript

field.text = "Website";  
var formatter:TextFormat = new TextFormat( );  
formatter.url = "http://www.rightactionscript.com/";  
formatter.target = "\_blank";  
field.setTextFormat(formatter);

加上颜色和下划线：

+展开

-ActionScript

field.text = "Website";  
var formatter:TextFormat = new TextFormat( );  
formatter.color = 0x0000FF;  
formatter.underline = true;  
formatter.url = "http://www.rightactionscript.com/";  
field.setTextFormat(formatter);

超链接可以是http或https协议，也可以是其他协议如邮件消息：

+展开

-ActionScript

field.text = "email";  
var formatter:TextFormat = new TextFormat( );  
formatter.color = 0x0000FF;  
formatter.underline = true;  
formatter.url = "mailto:joey@person13.com";  
field.setTextFormat(formatter);

用CSS是最完美的，它提供了超链接的a:link, a:active,和a:hover 样式：

+展开

-ActionScript

var css:StyleSheet = new StyleSheet( );  
css.parseCSS("a {color: #0000FF;} a:hover {text-decoration: underline;}");  
field.styleSheet = css;  
field.html = true;  
field.htmlText = "&lt;a href='http://www.rightactionscript.com'&gt;Website&lt;/a&gt;";