.getSelection()
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---
$(docu.....
$('#copy').click(function(){
var range = $('#TextArea').getSelection();
alert(range.text);
});
});
当按下#copy 按钮时,警报不会显示#t 中选定的文本。它是空白的。
我需要从文本区域中选择的文本
HTML code--
<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---
$(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.
I need the selected text from the textarea
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码未运行,因为此语句失败
在您提供的标记中没有任何
TextArea
作为 ID,因此脚本遇到错误并且不会继续执行。如果您将警报放在顶部,我确信警报框会弹出。
IE
Your code is not running because, this statement fails
There is nothing as
TextArea
as ID in the markup you provided, so the script encounters an error and does not continue beyond it.If you place the alert at the top part, I am sure the alert box will pop up.
i.e
getSelection
是文档的一个方法,所以你应该这样做:还要注意,你必须在 IE 中使用
document.selection.createRange()
所以一切都会变得有点复杂。请查看此示例了解更多信息。你最终会需要一个像这样的函数:
它应该返回选定的文本并在所有主要浏览器中工作。
编辑:
刚刚看到您正在使用某种 jquery 插件(也许)应该使您的代码正常工作。问题是:
在你的html中,textarea的id是“t”:
但是在你的javascript中,你试图获取id“TextArea”的选择:
请将你的textarea的id更改为“TextArea”(或反过来)看看会发生什么。
getSelection
is a method of the document, so you should do:also note that you'll have to use
document.selection.createRange()
in IE so everything gets a bit complicated.take a look at this example for more information. you'll end up needing a function like this:
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:
in your html, the id of the textarea is "t":
but in your javascript, you're trying to get the selection of id "TextArea":
please change the id of your textarea to "TextArea" (or the other way around) and see what happens.
我不确定这个问题,但如果您需要获取 textarea 值,只需使用
val
jQuery 方法:I'm not sure about the question, but if you need to get the textarea value, just use the
val
jQuery method:http://api.jquery.com/val/
您可以更改
id="t"
或将#TextArea
更改为#t
以获取 html 标记中的文本区域。但我不知道你正在使用什么插件或者它想要什么。
Either you change your
id="t"
or you change#TextArea
to#t
to get the textarea you have in your html markup.But I have no idea what plugin that you are using or what it want.