textarea雙擊按鈕在光標處插入內容

<!--html代碼-->
<div id="userdefinedTable"></div> 
<textarea id="custom_content" name="content" style="width: 98%; height: 100%; padding: 5px;"></textarea>

 

let title = "版本號";
//按鈕
let innerContent = $('<a href="javascript:void(0)" title="'+title+'" class="easyui-linkbutton btn_sm">'+title+'</a>');
$(innerContent).appendTo($("#userdefinedTable"));
bindAppendVar(innerContent,'custom_content');//綁定雙擊事件
//綁定雙擊事件
function bindAppendVar(obj,destObj){
	$(obj).dblclick(function(){
		var title = $(this).prop('title');
		title = "${"+title+"}";
		insertText(destObj,title);
	});
}
//光標位置插入字符
function insertText(destobj,str) {
	var obj = document.getElementById(destobj);
	if (document.selection) {
		var sel = document.selection.createRange();
		sel.text = str;
	} else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
		var startPos = obj.selectionStart,
			endPos = obj.selectionEnd,
			cursorPos = startPos,
			tmpStr = obj.value;
		obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
		cursorPos += str.length;
		obj.selectionStart = obj.selectionEnd = cursorPos;
	} else {
		obj.value += str;
	}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章