javascript .getSelection()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7416555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.getSelection()
提问by X10nD
HTML code--
HTML代码--
<script language="javascript" type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.field.selection.js"></script>
<div id="copy">Copy</div>
<textarea....id="t">
jquery---
jquery---
$(docu.....
$('#copy').click(function(){
var range = $('#TextArea').getSelection();
alert(range.text);
});
});
When the #copy button is pressed the alert does not show the selected text in #t. It comes in blank.
当按下#copy 按钮时,警报不会在#t 中显示所选文本。它是空白的。
I need the selected text from the textarea
我需要从 textarea 中选择的文本
回答by oezi
getSelection
is a method of the document, so you should do:
getSelection
是文档的一个方法,所以你应该这样做:
var range = document.getSelection();
also note that you'll have to use document.selection.createRange()
in IE so everything gets a bit complicated.
take a look at this examplefor more information. you'll end up needing a function like this:
还要注意,你必须document.selection.createRange()
在 IE 中使用,所以一切都会变得有点复杂。
请查看此示例以获取更多信息。你最终会需要这样的函数:
function getSelectedText(){
if(document.all){
var selection = document.selection;
var newRng = selection.createRange();
newRng.select();
return newRng.htmlText;
}else{
return document.getSelection();
}
}
wich should return the selected text and work in all major browsers.
至多应该返回选定的文本并在所有主要浏览器中工作。
EDIT:
just saw you're using some kind of jquery-plugin that (maybe) should make your code work. the problem is:
编辑:
刚刚看到您正在使用某种(也许)应该使您的代码工作的 jquery 插件。问题是:
in your html, the id of the textarea is "t":
在你的 html 中,textarea 的 id 是“t”:
<textarea....id="t">
but in your javascript, you're trying to get the selection of id "TextArea":
但是在您的 javascript 中,您试图选择 id“TextArea”:
$('#TextArea').getSelection();
please change the id of your textarea to "TextArea" (or the other way around) and see what happens.
请将您的 textarea 的 id 更改为“TextArea”(或其他方式),然后看看会发生什么。
回答by Starx
Your code is not running because, this statement fails
您的代码未运行,因为此语句失败
var range = $('#TextArea').getSelection();
There is nothing as TextArea
as ID in the markup you provided, so the script encounters an error and does not continue beyond it.
TextArea
您提供的标记中没有与ID 相同的内容,因此脚本遇到错误并且不会继续执行。
If you place the alert at the top part, I am sure the alert box will pop up. i.e
如果您将警报放在顶部,我肯定会弹出警报框。IE
$('#copy').click(function(){
alert(''); //this will work
var range = $('#TextArea').getSelection();
alert(range.text);
});
回答by Arnaud Leymet
I'm not sure about the question, but if you need to get the textarea value, just use the val
jQuery method:
我不确定这个问题,但如果您需要获取 textarea 值,只需使用val
jQuery 方法:
$('#copy').click(function(){
var range = $('#t').val();
alert(range);
});
回答by voigtan
Either you change your id="t"
or you change #TextArea
to #t
to get the textarea you have in your html markup.
您可以更改id="t"
或更改#TextArea
为#t
以获取 html 标记中的 textarea。
But I have no idea what plugin that you are using or what it want.
但我不知道你在使用什么插件或它想要什么。