Script
textarea 특정 위치에 원하는값 넣기
자바초보자
2015. 11. 16. 19:08
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