成功,附源码 flex cursor textarea 光标 位置 插入 insert caret

本文介绍了一个基于Flex的应用程序中短信模板配置界面的设计与实现。重点在于如何通过鼠标点击事件来设置短信模板中的动态参数,并展示了具体的ActionScript代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

			private function onConsumeSmsParamClicked(e:MouseEvent):void{
				this.consumeSmsTemplate.str = e.target.text;
				this.consumeSmsTemplate.focusManager.setFocus(this.consumeSmsTemplate);
				
			}

 

	<mx:HBox verticalAlign="bottom">
		<trade:TextAreaExt id ="prepaySmsTemplate"   styleName="input01" width="400" height="80" backgroundColor="#ffffff"/>
		<mx:Button label="预览内容" id="prepaySmsPreview" height="20" fontSize="12" fontWeight="normal"/>
	</mx:HBox>
			
	       <mx:HBox horizontalGap="0" id="prepaySmsParam" fontSize="12" fontWeight="normal">
        <mx:Label text="动态参数:"/>
        <mx:VBox verticalGap="0">
               <mx:HBox horizontalGap="3">
            <mx:Text text="会员姓名" styleName="lan" textDecoration="underline" fontWeight="normal" id="canshu11" mouseOver="canshu11.setStyle('textDecoration',null);" mouseOut="canshu11.setStyle('textDecoration','underline');" buttonMode="true" useHandCursor="true" mouseChildren="false"/>
            <mx:Text text="卡号后四位" styleName="lan" textDecoration="underline" fontWeight="normal" id="canshu12" mouseOver="canshu12.setStyle('textDecoration',null);" mouseOut="canshu12.setStyle('textDecoration','underline');" buttonMode="true" useHandCursor="true" mouseChildren="false"/>
            <mx:Text text="充值时间" styleName="lan" textDecoration="underline" fontWeight="normal" id="canshu13" mouseOver="canshu13.setStyle('textDecoration',null);" mouseOut="canshu13.setStyle('textDecoration','underline');" buttonMode="true" useHandCursor="true" mouseChildren="false"/>
            <mx:Text text="充值金额" styleName="lan" textDecoration="underline" fontWeight="normal" id="canshu15" mouseOver="canshu15.setStyle('textDecoration',null);" mouseOut="canshu15.setStyle('textDecoration','underline');" buttonMode="true" useHandCursor="true" mouseChildren="false"/>
           </mx:HBox>
            <mx:HBox horizontalGap="3">
            <mx:Text text="预存余额" styleName="lan" textDecoration="underline" fontWeight="normal" id="canshu16" mouseOver="canshu16.setStyle('textDecoration',null);" mouseOut="canshu16.setStyle('textDecoration','underline');" buttonMode="true" useHandCursor="true" mouseChildren="false"/>
             <mx:Text text="积分剩余数量" styleName="lan" textDecoration="underline" fontWeight="normal" id="canshu18" mouseOver="canshu18.setStyle('textDecoration',null);" mouseOut="canshu18.setStyle('textDecoration','underline');" buttonMode="true" useHandCursor="true" mouseChildren="false"/>
             <mx:Text text="优惠券剩余数量" styleName="lan" textDecoration="underline" fontWeight="normal" id="canshu19" mouseOver="canshu19.setStyle('textDecoration',null);" mouseOut="canshu19.setStyle('textDecoration','underline');" buttonMode="true" useHandCursor="true" mouseChildren="false"/>
             <mx:Text text="商户简称" styleName="lan" textDecoration="underline" fontWeight="normal" id="canshu20" mouseOver="canshu20.setStyle('textDecoration',null);" mouseOut="canshu20.setStyle('textDecoration','underline');" buttonMode="true" useHandCursor="true" mouseChildren="false"/>
            </mx:HBox>
            </mx:VBox>
        </mx:HBox>
 this.prepaySmsParam.addEventListener(MouseEvent.CLICK,onPrepaySmsParamClicked);
    
  

  

package com.tongcard.tcc.main.view.deskBack.sysManage.config.trade
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	
	import mx.controls.TextArea;

	public class TextAreaExt extends TextArea 
	{
	 	public var insertText:String = '';
		public function TextAreaExt()
		{
			super(); 
			this.addEventListener(FocusEvent.FOCUS_IN,onFocusIn);
		}

		private function onFocusIn(e:Event):void{
			trace(this.textField.caretIndex);
		 	this.textField.replaceText(this.textField.caretIndex,this.textField.caretIndex,insertText);
			this.insertText = '';
		}

	}
}

 

 

 

 

 

 

 

 

var insertIndex:int = tf.getCharIndexAtPoint( tf.mouseX, tf.mouseY );
tf.replaceText( insertIndex, insertIndex, "Here is some more text" );
啊哈,

http://www.flexdeveloper.eu/forums/flex-builder-flash-builder-eclipse/how-to-get-the-cursor-position-in-the-textarea/

textarea 光标位置插入是指在文本区域(textarea)中的光标位置插入新的文本或内容。 要实现在指定位置插入文本,可以通过以下步骤进行: 1. 获取文本区域的光标位置,可以使用textarea元素的selectionStart和selectionEnd属性来获取。 2. 将要插入的文本与原有文本分成两个段落,即左侧和右侧。 3. 将左侧段落与要插入的文本拼接起来,再拼接右侧段落,形成新的文本。 4. 将新的文本重新赋给文本区域。 以下是一个简单的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Textarea光标位置插入示例</title> <script> function insertText() { var textarea = document.getElementById("myTextarea"); var position = textarea.selectionStart; var textToInsert = "新的文本"; var originalText = textarea.value; var newText = originalText.slice(0, position) + textToInsert + originalText.slice(position); textarea.value = newText; } </script> </head> <body> <textarea id="myTextarea"></textarea> <button onclick="insertText()">在光标位置插入</button> </body> </html> ``` 在上述示例中,我们通过获取textarea元素的selectionStart属性获得光标位置,并使用slice函数将原有文本按照光标位置拆分成左右两个段落。接着,我们将新的文本插入到左侧段落中,并再次拼接上右侧段落,最后将新的文本赋给textarea的value属性即可。 这样,当点击按钮时,就能在光标位置插入指定的新文本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值