首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Javascript中获取SelectedText

在Javascript中获取SelectedText
EN

Stack Overflow用户
提问于 2015-04-15 21:14:17
回答 2查看 128关注 0票数 1

我想了解getSelection函数是如何工作的。所以我想做个小小的测试。我想在按钮上显示选定的文本。

我的代码:

代码语言:javascript
复制
<template name="seltext">
<textarea>This is a test test test test</textarea>
<input type="button" id="test"  class="new" value="pause">
</template>

Template.seltext.events({
  'click .new': function(){
    var text = "";
    if (window.getSelection) {
      text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
      text = document.selection.createRange().text;
    }
    return text;
    value = "text";
}});

我不知道为什么不管用,有人知道吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-15 21:57:26

选择是一个选择对象。当强制转换为string时,通过追加空字符串("")或使用String.toString(),这个对象就是选定的文本。这不适用于输入或文本区域。

票数 0
EN

Stack Overflow用户

发布于 2015-04-16 15:55:34

我搞定了,约书亚,你说得对,谢谢!此外,返回使行后过时。

代码语言:javascript
复制
<template name="seltext">
This is a test test test test
<input type="button" id="test"  class="new" value="pause">
</template>

Template.seltext.events({
  'click .new': function(){
    var text = "";
    if (window.getSelection) {
      text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
      text = document.selection.createRange().text;
    }
    $('#test').prop('value', text);
}});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29661029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档