gpt4 book ai didi

javascript - 在 Javascript 中获取选定文本

转载 作者:行者123 更新时间:2023-11-28 07:25:53 24 4
gpt4 key购买 nike

我想了解 getSelection 函数的工作原理。因此我想做一个小测试。我想在按钮上显示我选择的文本。

我的代码:

<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";
}});

我不知道为什么它不起作用,有人知道吗?

最佳答案

selection 是一个 Selection 对象。当转换为字符串时,无论是通过附加空字符串 ("") 还是使用 String.toString(),该对象都是选定的文本。这不适用于输入或文本区域。

关于javascript - 在 Javascript 中获取选定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29661029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com