2009-06-29 4 views
2

저는 그러한 속성에 익숙하지 않습니다.자바 스크립트로 textarea 요소 내에서 선택한 텍스트를 가져 오는 방법은 무엇입니까?

누군가 간단한 데모를 제공 할 수 있습니까?

라이브러리가 없어도됩니다.

+3

스택 오버플로 아카이브 - http://stackoverflow.com/questions/717224/how-to-get-selected-text-in-textarea - http://stackoverflow.com/questions/275761/how- textbox-control-with-js에서 텍스트를 가져와야하는 경우 --- ** 힌트 ** : 질문을하기 전에 Google을 살펴보십시오 :'site : stackoverflow.com javascript textarea에서 선택한 텍스트 가져 오기 '는 stackoverflow.com에서 관련 자료. 또는 주소 표시 줄에 다음과 같은 것을 입력 할 수 있습니까? 첫 번째 결과로 이동합니다. 한 번 줘, 주소 표시 줄에 입력하십시오 :'? site : stackoverflow.com javascript textarea 내 선택한 텍스트를 얻을' – Sampson

답변

6
<script type="text/javascript"> 

     function ShowSelectionInsideTextarea() 
{ 
var textComponent = document.getElementById('mytextarea'); 

    var selectedText; 
    // IE version 
    if (document.selection != undefined) 
    { 
    textComponent.focus(); 
    var sel = document.selection.createRange(); 
    selectedText = sel.text; 
    } 
    // Mozilla version 
    else if (textComponent.selectionStart != undefined) 
    { 
    var startPos = textComponent.selectionStart; 
    var endPos = textComponent.selectionEnd; 
    selectedText = textComponent.value.substring(startPos, endPos) 
    } 
    alert("You selected: " + selectedText); 

} 
</script> 
관련 문제