2012-03-18 4 views
1

내 응용 프로그램은 마우스로 굵게 선택한 텍스트를 선택하도록하고 싶습니다. 자바 스크립트를 사용하여 어떻게해야합니까?자바 스크립트에서 마우스를 사용하여 선택한 텍스트를 알면


function getSelText() 
{ 
    var txt = ''; 
    if (window.getSelection) 
    { 
     txt = window.getSelection(); 
    } 
    else if (document.getSelection) 
    { 
     txt = document.getSelection(); 
    } 
    else if (document.selection) 
    { 
     txt = document.selection.createRange().text; 
    } 
    else { return; } 
} 
//txt is the selected text 
: 또한 방법 예를 들어, 난 그냥 커서가이 같은 것을 의미합니까
+0

무엇에 삽입할까요? 온라인 편집자? – mplungjan

답변

3

텍스트 영역에서이 작업을 수행 할 수 있습니다.

<html> 
<head> 

<title>onselect test</title> 

<script type="text/javascript"> 

window.onselect = selectText; 

function selectText(e) 
{ 
    start = e.target.selectionStart; 
    end = e.target.selectionEnd; 
    alert(e.target.value.substring(start, end)); 
} 
</script> 
</head> 

<body> 
<textarea> 
Highlight some of this text 
with the mouse pointer 
to fire the onselect event. 
</textarea> 
</body> 
</html> 
1

배치되는 텍스트 전에 내 함수를 사용하여 텍스트를 삽입해야 할 수도 있습니다 ... 자바 스크립트를 사용하여 커서 위치를 알고
+0

그 이유는 무엇입니까? 어떤 브라우저를 지원합니까? – gdoron

+0

선택을 할 때 이벤트 처리기가 있는지 ... 확인할 것입니다 ... onSelecte 등 ... –