在 Javascript 中获取 SelectedText
Get SelectedText in Javascript
我想了解 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() 转换为字符串时,此对象是选定的文本。这不适用于输入或文本区域。
我成功了,Joshua 你说的对,谢谢! return 也使该行过时。
<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);
}});
我想了解 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() 转换为字符串时,此对象是选定的文本。这不适用于输入或文本区域。
我成功了,Joshua 你说的对,谢谢! return 也使该行过时。
<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);
}});