2012-10-15 2 views
1

textarea에서 5 번째 줄 이후의 모든 문자가 삭제 될 수있는 기능이 필요합니다. 번째 줄에는 최대 15 개의 문자 만 포함됩니다.5 번째 줄 이후의 텍스트 영역에서 문자 삭제

내가 "캐럿"을 사용하여이 조금을 달성 할 수 있지만, 그것은 단지 kepress 이벤트 및 텍스트 영역에서 여분의 텍스트를 지 웁니다하여 setInterval (텍스트 영역의 onfocus 및) 함수 를 사용하여, 특정 커서 위치에 메신저 텍스트를 추가하며, 됨 clearInterval (텍스트 영역의 및 onblur)은 광고를 포함

function insertTextAtCaret(el, text) 
{ 
    var val = el.value, endIndex, range; 
    if (typeof el.selectionStart != "undefined" && typeof el.selectionEnd != "undefined") { 
     endIndex = el.selectionEnd; 
     el.value = val.slice(0, endIndex) + text + val.slice(endIndex); 
     el.selectionStart = el.selectionEnd = endIndex + text.length; 
    } else if (typeof document.selection != "undefined" && typeof document.selection.createRange != "undefined") { 
     el.focus(); 
     range = document.selection.createRange(); 
     range.collapse(false); 
     range.text = text; 
     range.select(); 
    } 
} 
+1

선 배치로 인해 중단? –

+0

네, 한 줄에 15 자 뒤에 \ n을 삽입하고 최대 총 5 줄이 필요합니다. – SML

+0

그리고 시작 색인이있는 텍스트를 삭제하는 방법에 대해 고민하고 계신가요? –

답변

1
function charCountTextarea(textAreaId,e,limit,lineLen) 
{ 

    var code = e.charCode || e.keyCode; 

    newLines = $("#"+textAreaId).val().split("\n").length; 
    var t = $("#"+textAreaId)[0]; 
    var lineIndex = t.value.substr(0, t.selectionStart).split("\n").length-1; 
    console.log('val t :'+$("#"+textAreaId).val()+' line index : '+lineIndex+' new lines '+newLines); 

    if(lineIndex == 10 && $("#"+textAreaId).val().split("\n")[lineIndex].length>(lineLen+1) && code!=8 && code!=46 && code!=33 && code!=34 && code!=35 && code!=36 && code!=37 && code!=38 && code!=39 && code!=40) 
    { 
     $("#"+textAreaId).val(($("#"+textAreaId).val()).substring(0, $("#"+textAreaId).val().length - 1)); 
     alert('You are reached to limit'); 
     return false; 
    } 

    if(lineIndex<5) 
    { 
     $("#"+textAreaId).val($("#"+textAreaId).val().wordWrap(lineLen, "\n", 0)); 
    } 
    var countLine1 = $("#"+textAreaId).val().split("\n")[0].length; 

    if($("#"+textAreaId).val().split("\n")[lineIndex].length>lineLen) // which will count the total line char condition 
    { 
     console.log("In condition : "); 
     if(code!=8 && code!=46 && code!=33 && code!=34 && code!=35 && code!=36 && code!=37 && code!=38 && code!=39 && code!=40) 
     { 
      // to insert next line    
      insertTextAtCaret(document.getElementById(textAreaId), "\n"); 
     } 
    } 
} 
+0

Ek number re sunya – Freelancer

+0

dhanywad re sagya – SML

관련 문제