开发者

Event action on clicking a hyperlink from a textbox

Restating my question to be simpler....

I want to be able to link an Action Script event to clicking text within a text box.The only thing I can see is to just create a basic Hyper Link, but not apply any action:

Event action on clicking a hyperlink from a textbox

I have been messing around for over an hour but just can't see a way to apply actionscript and all the tutorials on the internet seem to target ActionScript 3 or not do exactly what I want.

The reason for this is that there is background music to the site and when YouTube is launched, it needs to be muted. I know the code to mute and have done this on custom objects before, but I can't see any way to apply script to a textbox hyperlink.

开发者_StackOverflow中文版

Whilst I would ideally like to do it this way, I am happy to consider any solution resulting in opening a page and muting the site.

To be honest, I tried doing a quick switch to AS3, but as there are so many problems that would need addressing, I would rather spend the time converting the site to HTML/Jquery or even Silverlight.... I just hope there is something small I have overlooked which can get this done without too many changes needed.


You can you the TextEvent.LINK event to listen for user clicks on a link in a textfield:

import flash.text.TextField;
import flash.events.TextEvent;

var textField:TextField = new TextField();
textField.htmlText = "<a href='event:arg1,arg2'><b>hyperlink</b></a>";
addChild(textField);

textField.addEventListener(TextEvent.LINK, onTextFieldLink);

function onTextFieldLink(e:TextEvent):void
{
    var args:Array = e.text.split(",");
    trace(args); // output: arg1, arg2

}// end function

Any parts that come after "event:" are stored in the event object's text property. You can simulate parsing arguments to the event object using the String.split() method to split the text string with a delimiter like a comma. Then you can store each indvidual element in an array.

[UPDATE]

For your particular scenario(as far as I understand it), the following may be better suited for you:

import flash.text.TextField;
import flash.events.TextEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;

var textField:TextField = new TextField();
textField.htmlText = "<a href='event:watch?v=JZweDwbJ_Ic'><b>hyperlink</b></a>";
addChild(textField);

textField.addEventListener(TextEvent.LINK, onTextFieldLink);

function onTextFieldLink(e:TextEvent):void
{
    var url:String = "http://www.youtube.com/" + e.text;
    var urlRequest:URLRequest = new URLRequest(url);
    navigateToURL(urlRequest, "_blank");

}// end function


You can also use TextArea class (NOT TEXTAREA THE COMPONENT! it's a brand new OOP class created by doitflash) instead of using TextField class every where you need to work with text in your project!

check out www.doitflash.com

The site produced a class named "TextArea" which is an extension to the original "TextField" class that ends to the shortcomings of TextField.

TextArea allows you to call your own custom functions right from the hyperlink inside your text block rather than just calling external links or confusing yourself by adding listener when you click the hyperlink, etc... because what if when you want to even pass arguments through your function from your text block, that's hard but TextArea answers to that too :)

Here is a sample code to call a function by hyperlink inside your text block:

import flash.text.TextFieldAutoSize;
import com.doitflash.text.TextArea;



// set TextArea
var _textArea:TextArea = new TextArea();
_textArea.condenseWhite = true;
_textArea.autoSize = TextFieldAutoSize.LEFT;
_textArea.embedFonts = false;
_textArea.border = true;
_textArea.multiline = true;
_textArea.wordWrap = true;
_textArea.width = 200;

_textArea.holder = this;
_textArea.client = this; // must be where you have your 'allowed functions' saved
_textArea.funcSecurity = true;
_textArea.allowedFunctions(stringLink, objectLink, arrayLink, arrayObjectStringLink);
_textArea.mouseRollOverEnabled = true;
_textArea.fmlText = "<p>Pass String as arguments in this <a href='event:stringLink(simple string)'>link</a>.</p>";



/*
my custom functions that I call from _textArea.fmlText by using <a /> tags, like we used to insert hyperlinks inside our text blocks.
for example, to call stringLink() from _textArea.fmlText, you can write: 
_textArea.fmlText = "my hyperlink: <a href='event:stringLink(this is my passed value)'>link</a>.";

NOTE: your custom functions arguments can be as many as you like (supported argument types: Object, Array, String)
*/
function stringLink($value:String):void
{
    trace("custom function");
}

It has lots of more features and doesn't specifically limited to this issue. Check out doitflash for more information. the site provides the platform and downloading it is also free of charge :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜