用js控制按钮事件,将指定内容添加到文本域的光标所在位置。

<pre class="javascript" name="code">	
	var fm = document.getElementsByTagName('form')[0];
	var button = document.getElementsByName("button");
	var textcontent = document.getElementById('textcontent');
	//获取一个cookie的值
	function getCookie(index){
		var allcookies = document.cookie;  
		var cookie_pos = allcookies.indexOf(index);
		if (cookie_pos != -1){
			cookie_pos += index.length + 1;
			var cookie_end = allcookies.indexOf(";",cookie_pos); 
			if (cookie_end == -1){  
  				cookie_end = allcookies.length;  
			}  
			var value = unescape(allcookies.substring(cookie_pos, cookie_end)); 
		}    
		return value;  		
	}
	//获取文本中的光标位置
	 function getCursortPosition (ctrl) {
		var CaretPos = 0;	// IE Support
		if (document.selection) {
		ctrl.focus ();
			var Sel = document.selection.createRange ();
			Sel.moveStart ('character', -ctrl.value.length);
			CaretPos = Sel.text.length;
		}
		else if (ctrl.selectionStart || ctrl.selectionStart == '0')
			CaretPos = ctrl.selectionStart;
		return (CaretPos);
	}

 
	//获取文本光标位置并设置一个cookie来存储光标时时变动的位置的函数
	var textpos = 0;
	textcontent.onclick  = function(){
		textpos = getCursortPosition (textcontent);
		document.cookie="textpos="+textpos;		
		textpos = getCookie("textpos");
	}
	//字符串的写入函数
	function content(string){	
		s = textcontent.value;
		fm.content.value  = s.substring(0,getCookie("textpos"))+string+s.substring(getCookie("textpos"));
	}
<pre class="php" name="code">	//点击按钮向表单中的文本域的光标处添加内容
	button[0].onclick  = function(){
		content('[b][/b]');
	}


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值