function ShowSelection()
{
var textComponent = document.getElementById('TextArea1');
var selectedText;
if (document.selection != undefined) {
textComponent.focus();
var sel = document.selection.createRange();
selectedText = sel.text;
}
else if (textComponent.selectionStart != undefined) {
var startPos = textComponent.selectionStart;
var endPos = textComponent.selectionEnd;
selectedText = textComponent.value.substring(startPos,endPos)
}
alert("You selected: " + selectedText)
}
</script>
<asp:TextBox id="TextArea1" TextMode="MultiLine" runat="server">/asp:TextBox>
<a href="#" onclick=alert(ShowSelection());>Click here to display the selected text</a>
여기에서 사용자가 선택한 텍스트를 표시하려고합니다. 사용자가 선택한 텍스트를 표시해야합니다.하지만 아쉽게도 이벤트가 트리거되지 않습니다.이 코드에 오류가 있습니다. 해결책을주세요 ..
asp.net이 아닌 fiddle에서 동일한 코드가 저에게 효과적이었습니다.
<script>
function ShowSelection() {
var textComponent = document.getElementById('TextArea1');
var selectedText;
if (document.selection != undefined) {
textComponent.focus();
var sel = document.selection.createRange();
selectedText = sel.text;
} else if (textComponent.selectionStart != undefined) {
var startPos = textComponent.selectionStart;
var endPos = textComponent.selectionEnd;
selectedText = textComponent.value.substring(startPos, endPos)
}
alert("You selected: " + selectedText)
}
</script>
<input id="TextArea1" type="textarea" />
<a href="ShowSelection()" onclick="ShowSelection()">click me </a>
요점은 ShowSelection()
함수에 대해 경고를 다시 호출하는 이유입니다 . 해당 함수 자체는 문자열 값을 반환하지 않고 경고합니다.
확인해주십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다