본문 바로가기
Script

textarea 특정 위치에 원하는값 넣기

by 자바초보자 2015. 11. 16.
728x90
	function jf_htmlElementAdd(type){
		//$("#html").focus();
		set_tag_support($('#html'), '이거추가함!!');
		
	}
	
	function set_tag_support(obj, prefix, postfix) {
		if(postfix == null){
			postfix = "";
		}
		if (document.getSelection)    ts = document.getSelection();
		else if (document.selection)  ts = document.selection.createRange().text;
		else if (window.getSelection) ts = window.getSelection();
	
		if (ts != ""){ //IE
			document.selection.createRange().text = prefix + ts + postfix;
		}else {
			if (obj.selectionStart == obj.selectionEnd){
				if(/*@cc_on!@*/false){
					obj.focus();
					document.selection.createRange().duplicate().text = prefix + document.selection.createRange().duplicate().text + postfix;
					document.selection.createRange().select();
				}else{
					var s1 = obj.value.substring(0, obj.selectionStart);
					var s2 = obj.value.substring(obj.selectionStart);
					obj.value = s1 + prefix + postfix + s2;
				}
			}else{
				var s1 = obj.value.substring(0, obj.selectionStart);
				var s2 = obj.value.substring(obj.selectionStart, obj.selectionEnd);
				var s3 = obj.value.substring(obj.selectionEnd);
				obj.value = s1 + prefix + s2 + postfix + s3;
			}
		}
		obj.focus();
	}


 

 


	function jf_setTextByCursor(obj, txt){
		var selectionStart = $(obj)[0].selectionStart;
		var selectionEnd = $(obj)[0].selectionEnd;
		
		var objTxt = $(obj).val();
		var tempTxt = objTxt.substring(0,selectionStart);
		tempTxt = tempTxt + txt;
		tempTxt = tempTxt + objTxt.substring(selectionEnd);
		$(obj).val(tempTxt);
		$(obj).selectRange(selectionStart + txt.length, selectionStart + txt.length);

	}

	$.fn.selectRange = function(start, end) {

		return this.each(function() {

			if(this.setSelectionRange) {

				//this.focus();

				this.setSelectionRange(start, end);

			}

			else if(this.createTextRange) {

				var range = this.createTextRange();

				range.collapse(true);

				range.moveEnd('character', end);

				range.moveStart('character', start);

				range.select();

			}

		});

	};
728x90

'Script' 카테고리의 다른 글

문자, 숫자 정렬  (0) 2015.12.11
도메인 정보  (0) 2015.12.09
string boolean number 형변환  (0) 2015.10.30
인터넷 익스플로러 교차스크립트 xss 기능 동작 우회 방법  (0) 2015.10.28
브라우저 구분  (0) 2015.10.28