开发者

Convert javascript code to Action script code?

What is 开发者_如何学编程the equivalenet of the following code in Flex/AS3?

var elem = document.getElementById('hiddenTestDiv');
elem.innerHTML = '<b><span class="redText">H</span>ello <span>World!</span></b>';
var innerText  = elem.innerText;  // equals 'Hello World!'
var contentLength = elem.innerText.length; // equals 12


I am not getting basic purpose of your question clearly, i.e. Traversing to speific element and getting it content/text as well as its content/text lenght. if so, this sample may be helpful

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    creationComplete="{getElementByID('txt', this)}">
    <mx:Script>
        <![CDATA[
            import mx.controls.Text;
            import mx.core.Container;

            import mx.core.UIComponent;
            import mx.controls.Alert;

            private function getElementByID(name:String, container:Container):void
            {
                 var child:DisplayObject = container.getChildByName(name);

                 if (child != null)
                 {
                    Alert.show(Text(child).text+", Length :"+Text(child).text.length.toString());
                    return;
                 }
                 for each(var childContainer:Container in container.getChildren())
                 {
                    getElementByID(name, childContainer);
                 }

            }



        ]]>
    </mx:Script>

    <mx:HBox id="hBox" >
        <mx:Text text="Hello World" name="txt"/>
    </mx:HBox>

</mx:Application>

In this Sample function getElementByID gets the control by given name in hierarchy of given container example using Application

hopes that helps


Short answer, there is no 'proper' way to convert Javascript to Flex. You have to understand that AS3 is object oriented and Javascript isn't. The base concept behind to two is also VERY different (display list, timeline, frames, events, binding, etc).

The only resemblance between the two is that actionscript is based loosely on ECMAScript and so is Javascript.

For this particular case however, it seems that you're trying to bind new data to a label, but I can't give you any example code because of lack of specifics.


Note that you could put some limited html within the string assigned to the htmlText property. In the example below I set the bold, font color properties in the mxml component. That could be done in the html or with actionscript.

    <mx:Script><![CDATA[

       function setHiddenText():void{
         hiddenTestText.htmlText = "H";
         hiddenTestText.htmlText = "ello World";
         var contentLength:Number = hiddenTestText.htmlText.length + hiddenTestText.htmlText.length;
       }

        ]]></mx:Script>


    <mx:HBox>
  <mx:Text id="hiddenTestText" fontWeight="bold" color="red"/>
  <mx:Text id="hiddenTestText2" fontWeight="bold"/>
</mx:HBox>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜