이 문제를 해결할 더 좋은 방법이있을 것이라고 확신합니다. 다른 사람도이 문제에 대해 밝히기를 바랍니다. 이 솔루션은 범위를 옮기기 위해 시행 착오를 사용합니다. 일이 잘못되면 원래 상태로 되돌아갑니다.
도움이 되었기를 바랍니다.
<html>
<body>
<script>
window.setInterval(
function() {
var selection = document.selection;
var range = document.selection.createRange();
var parentEl = range.parentElement();
// Make a duplicate range to revert to if things go wrong.
range2 = range.duplicate();
range.moveStart('character', 1);
if (range.parentElement() != parentEl) {
// We've left the original parent (which is bad), so revert to the original range. We're probably at the end of the textarea.
range = range2.duplicate();
}
range.moveStart('word', -1);
// Make a new duplicate range to revert to.
range2 = range.duplicate();
// Move the end of the range one backwards, then forward to the end of the word.
range.moveEnd('character', -1);
range.moveEnd('word', 1);
if (range.parentElement() != parentEl) {
range = range2.duplicate();
while (range2.parentElement() == parentEl) {
range.moveEnd('character', 1);
range2 = range.duplicate();
}
}
var wort = range.text.replace(/^\s\s*$/, '').replace(/\s\s*$/, '');
document.getElementById("ausgabe").innerHTML = wort;
}, 100
)
</script>
<textarea id="ta" rows="10" cols="40">At vero eos et accu-samus et iusto? Odio, dignissimos. ducimus qui bländitiis praeséntium voluptatèm deleniti atque corrupti quos</textarea>
<p>[<span id="ausgabe"></span>]</p>
</body>
</html>
: 여기
코드입니다