文本选择扩展后如何在iOS中捕获所选文本范围 [英] How to captured Selected Text Range in iOS after text selection expansion

查看:144
本文介绍了文本选择扩展后如何在iOS中捕获所选文本范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个网络应用程序,允许用户选择一些文本,单击一个按钮,并保存突出显示的文本。这在桌面浏览器(在本例中为chrome)中非常出色,但在iOS中我遇到了本地文本选择问题,用户可以在其中更改所选文本。



这是显示问题的JsFiddle(问题仅存在于iOS中):

  $(function(){
$ ('#actionButton')。click(function(){
if(selectedRange){
$('#result')。text(selectedRange.toString());
clearInterval );
}
});

timer = setInterval(getSelectedRange,150);
});

var timer = null;

var selectedRange = null;

var getSelectedRange = function(){
try {
if(window.getSelection){
selectedRange = window.getSelection()。getRangeAt(0);
} else {
selectedRange = document.getSelection()。getRangeAt(0);
}
} catch(err){

}

};


I'm working on a web app that allows a user to select some text, click a button, and save the highlighted text. This works great in desktop browsers, (chrome for this example), but in iOS I'm having issues with the native text selection, where the user can change the selected text.

Here is the JsFiddle showing the issue (issue only exists in iOS): http://jsfiddle.net/JasonMore/gWZfb/

  1. User starts text selection

  2. User expands their text selection, and clicks "Show the selected text above"

  3. Only the first selected word "The" shows up, even though I want "The Path of the righteous man"

1 Begin

2 Select Text and hit button

3 Only "The"

Here is the JS I am using:

$(function() {
    $('#actionButton').click(function() {
        $('#result').text(selectedRange.toString());
    });

    $('#slipsum').on('mouseup touchend','p', function() { 
        getSelectedRange();
    });
});

var selectedRange = null;

var getSelectedRange = function() {
    if (window.getSelection) {
        selectedRange = window.getSelection().getRangeAt(0);
    } else {
        selectedRange = document.getSelection().getRangeAt(0);
    }
};​

HTML:

<h3>Selected Text:</h3>
<p id="result">
</p>
<br/>
<p>
    <input type="button" id="actionButton" value="Show the selected text above" />
</p>
<!-- start slipsum code -->
<div id="slipsum">
<h1>Is she dead, yes or no?</h1>
<p>Do you see any Teletubbies in here? Do you see a slender plastic tag clipped to my shirt with my name printed on it? Do you see a little Asian child with a blank expression on his face sitting outside on a mechanical helicopter that shakes when you put quarters in it? No? Well, that's what you see at a toy store. And you must think you're in a toy store, because you're here shopping for an infant named Jeb. </p>

<h1>So, you cold?</h1>
<p>The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee. </p>

<h1>I'm serious as a heart attack</h1>
<p>Do you see any Teletubbies in here? Do you see a slender plastic tag clipped to my shirt with my name printed on it? Do you see a little Asian child with a blank expression on his face sitting outside on a mechanical helicopter that shakes when you put quarters in it? No? Well, that's what you see at a toy store. And you must think you're in a toy store, because you're here shopping for an infant named Jeb. </p>

<h1>Is she dead, yes or no?</h1>
<p>Like you, I used to think the world was this great place where everybody lived by the same standards I did, then some kid with a nail showed me I was living in his world, a world where chaos rules not order, a world where righteousness is not rewarded. That's Cesar's world, and if you're not willing to play by his rules, then you're gonna have to pay the price. </p>

<h1>Is she dead, yes or no?</h1>
<p>Your bones don't break, mine do. That's clear. Your cells react to bacteria and viruses differently than mine. You don't get sick, I do. That's also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke. We get some in our lungs, we drown. However unreal it may seem, we are connected, you and I. We're on the same curve, just on opposite ends. </p>
</div>
<!-- please do not remove this line -->

<div style="display:none;">
<a href="http://slipsum.com">lorem ipsum</a></div>

<!-- end slipsum code -->
​

解决方案

To anyone who stumbles upon this issue in the future, here is the resolution:

http://jsfiddle.net/JasonMore/gWZfb/

$(function() {
    $('#actionButton').click(function() {
        if (selectedRange) {
            $('#result').text(selectedRange.toString());
            clearInterval(timer);
        }
    });

    timer = setInterval(getSelectedRange, 150);
});

var timer = null;

var selectedRange = null;

var getSelectedRange = function() {
    try {
        if (window.getSelection) {
            selectedRange = window.getSelection().getRangeAt(0);
        } else {
            selectedRange = document.getSelection().getRangeAt(0);
        }
    } catch (err) {

    }

};​

这篇关于文本选择扩展后如何在iOS中捕获所选文本范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆