我想了解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";
}});我不知道为什么不管用,有人知道吗?
发布于 2015-04-15 21:57:26
选择是一个选择对象。当强制转换为string时,通过追加空字符串("")或使用String.toString(),这个对象就是选定的文本。这不适用于输入或文本区域。
发布于 2015-04-16 15:55:34
我搞定了,约书亚,你说得对,谢谢!此外,返回使行后过时。
<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);
}});https://stackoverflow.com/questions/29661029
复制相似问题