FLEX:How to catch 'a href' event
I'm loading data from TextFlow to spark:TextArea. In TextFlow i have 'a href' elements. Problem lies with the address of the 'link' elements. Some of them will go outside of the page (these i will leave without doing anything), other will go to other subpages in my site(these one i have to catch and process in code).
I planned on catching 'clicking on link' event to decide what to do, but i can't do this. I read tha开发者_如何学Ct by writing
<a href="event:page_adress">
i could catch the event on link:event in old mx:TextArea, but i cant do this here. I tried to manualy add:
addEventListener(TextEvent.Link, functionName)
but it doesnt work. When i change to catching MOUSE_CLICK event i'm catching the event, but cant parse it to get the needed address.
Anybody have idea how to do it? I doesnt have to stick with TextArea, but component have to download content form TextFlow.
PS I noticed that when i added event:page_adress to the 'a href' the link stopped working (that means it probably started throwing events), but i cant catch it...
PS2 TextFlow is imported from external database with
TextFlowUtil.importFromXML
i tried using in link element, click event but it doesnt work:
<a click="myClickHandler(event)">
myClickHandler doesnt run after clicking on link...
OK i found answer on this blog: http://flexdevtips.blogspot.com/2010/10/displaying-html-text-in-labels.html
Bassicly i had to search whole textFlow and in case of founding a Link - adding manually Event to it.
Im posting my code in case that the flexdevtips blog might be down in the future:
/**
* Finds the first LinkElement recursively and returns it.
*/
private function findLinkElement(group:FlowGroupElement):void
{
var childGroups:Array = [];
// First check all the child elements of the current group,
// Also save any children that are FlowGroupElement
for (var i:int = 0; i < group.numChildren; i++) {
var element:FlowElement = group.getChildAt(i);
if (element is LinkElement)
{
linksArray.push(element as LinkElement);
} else if (element is FlowGroupElement)
{
childGroups.push(element);
}
}
// Recursively check the child FlowGroupElements now
for (i = 0; i < childGroups.length; i++) {
var childGroup:FlowGroupElement = childGroups[i];
findLinkElement(childGroup);
}
}
and i had to use it in my code like this:
linksArray = [];
findLinkElement(tt.textFlow);
var iter:int=0;
for (iter = 0 ; iter<linksArray.length ; iter++)
{
linksArray[iter].addEventListener(FlowElementMouseEvent.MOUSE_DOWN,linkSelect, false, 0, true);
}
where tt is my textarea with imported textFlow
Maybe this helps. I use a html-link like this:
<a id="link_1"> Click me </a>
The html-file is stored externally. I import the html to a TextFlow using importToFlow and add the Listener in Flex, like this:
myTextFlow.addEventListener(FlowElementMouseEvent.ROLL_OVER, showMe);
To get the id-property of the link I use:
event.flowElement.id
just found out how to get the original string:
function onClick(event:FlowElementMouseEvent):void
{
var link:LinkElement = event.flowElement as LinkElement;
var text:String = link.href;
}
this will return the content of the tag, so you can parse to get your parameters
thanks for the post, it helped me a lot. at least i got one step further, i can actually listen to the events.
but i still have a problem, the HTML code i set on my RichEditableText looks like this:
<a href='event:n," + id + "'>Name</a>
on Flex3 i could use the TextEvent.LINK get the arguments by its 'text' property, then parse to get the ID data that i need. but now that we are developing in Flex4 i cannot figure out how to do this. after catching the FlowElementMouseEvent i can only get the link text ('Name'), but not the arguments im sending on the HTML code.
do you have any suggestions on how to do this?
精彩评论