如何在window.getSelection中选择多个单词
如何在 window.getSelection
中选择多个单词。
在此代码中,如果我选择 tweenen paragrap
,它将锁定单词 paragraphs
并错过 ` Between 。我需要选择两个单词。
或者即使我选择3个词dd 2 spa
,它也会选择3个词add 2 space
。谢谢。
<script type="text/javascript">
function Selected() {
var sel;
if ((window.getSelection) && (sel = window.getSelection()).modify) {
sel = window.getSelection();
if (!sel.isCollapsed) {
sel.modify("move", "backward", "word");
sel.modify("extend", "forward", "word");
}
}
}
</script>
<p>put returns between paragraphs, for linebreak add 2 spaces at end</p>
<input type="button" onclick="Selected();" value="selected">
How to select multi words in window.getSelection
.
In this code, if I choose tweenen paragrap
, it will locked word paragraphs
and missed `between. And I need select both 2 words.
Or even I choose 3 words dd 2 spa
, it will select 3 words add 2 spaces
. thanks.
<script type="text/javascript">
function Selected() {
var sel;
if ((window.getSelection) && (sel = window.getSelection()).modify) {
sel = window.getSelection();
if (!sel.isCollapsed) {
sel.modify("move", "backward", "word");
sel.modify("extend", "forward", "word");
}
}
}
</script>
<p>put returns between paragraphs, for linebreak add 2 spaces at end</p>
<input type="button" onclick="Selected();" value="selected">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您希望该按钮将所选内容对齐到整个单词。如果是这样,您可以这样做:
现场演示:http://jsfiddle.net/uCHVQ/1/< /a>
代码:(
顺便问一下,该代码最初是来自我发布的内容吗?它看起来很熟悉,但我找不到它)。
If I understand you correctly, you want the button to snap the selection to whole words. If so, here's how you can do it:
Live demo: http://jsfiddle.net/uCHVQ/1/
Code:
(By the way, is that code originally from something I've posted? It looks familiar but I can't find it).