使用Javascript在IPad 2上获取所选文本 [英] Get selected text on IPad 2 with Javascript
问题描述
我正在开发一个Web应用程序,它允许选择html文档的一部分并在其上添加某种注释。
要获取所选文本,我使用 window.getSelection()
,这在IE9,FF和Safari中运行良好。
但是当我在我的iPad 2上使用相同的页面时遇到麻烦:
I'am developing a web-application that allows to select parts of an html document and put some kind of annotation on it.
To get hold of the selected text I use window.getSelection()
which works pretty fine in IE9, FF, and Safari.
However I run into trouble when using the same page on my IPad 2:
- 如果我只是通过点击它来选择一个单词一秒钟,
window.getSelection()
返回正确的选择。 - 如果我创建一个文本范围(如此处所述 http://blog.laptopmag.com/how -to-select-copy-and-paste-on-the-ipad )总是返回null。
我已经检查了窗口,文档和相关事件对象 - 但没有成功......
- If I just select a word by tapping it for a sec,
window.getSelection()
returns the proper selection. - If I create a text range ( as discribed here http://blog.laptopmag.com/how-to-select-copy-and-paste-text-on-the-ipad ) always return "null". I've already examined the window, document and related event objects - but without success...
任何帮助都会非常感谢!
Any help would be really appreciated!
编辑:只是一个小例子。选择文本,然后按按钮。在Safari(PC)上,该功能打印所选值...
Just a small example. Select a text and press the button. On Safari (PC) the function prints the selected value...
<html>
<body>
<script>
function a()
{
alert(window.getSelection());
}
</script>
Hello World! <input type="button" onclick="a();"
</body>
</html>
推荐答案
好吧我终于解决了这个问题:作为蒂姆假设点击事件导致选择崩溃。我认为这是相当奇怪的行为,因为在常规Safari上这不会发生。
Okay finally I've solved the problem: As Tim assumed the click events causes to selection to collapse. I consider this as rather strange behavior as on regular Safari this does not happen.
但是,解决方案不是使用click事件。而不是我使用jquery mobile提供的vlick。
However, the solution is not to use the click event. Instead of I'm using "vlick" provided by the jquery mobile.
完整的工作示例:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
</head>
<body>
Hello World! <input type="button" id="button1" />
<script>
function a()
{
alert(window.getSelection());
}
$("#button1").bind("vclick",a);
</script>
</body>
</html>
这篇关于使用Javascript在IPad 2上获取所选文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!