我工作的一個Web應用程序,允許用戶選擇一些文本,點擊一個按鈕,並保存突出顯示的文本。這在桌面瀏覽器中效果很好,(在本例中爲chrome),但在iOS中,我遇到了本地文本選擇方面的問題,用戶可以在其中更改所選文本。如何捕捉選定文本範圍中的iOS之後的文本選擇擴展
這裏是的jsfiddle顯示問題(問題僅在iOS的存在):http://jsfiddle.net/JasonMore/gWZfb/
用戶開始文本選擇
用戶擴展他們的文本選擇,並點擊「顯示上面選定的文本「
只有第一個選定的單詞」The「出現了,即使我想要」右邊的路徑的男人「
1開始![begin iOS text selection](https://i.stack.imgur.com/Nketj.png)
2選擇文本,然後按按鈕![expand text](https://i.stack.imgur.com/tYqbW.png)
3只 」的「 ![Only The](https://i.stack.imgur.com/roPJd.png)
這裏是JS,我使用:
$(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 -->
給這個jQuery插件試一試,看看它是否工作的http:// jamesmgreene .github.com/jquery.text選擇/ –
有趣的是,我坐在詹姆斯身後。你也在伊根工作嗎? –
鼠標和mousedowns不會轉換爲iOS。我改變了他們的觸覺和傾向,並且有更接近的東西。謝謝 –