記事タグにテキストを挿入します。記事タグはdivタグ内にあります。次に、マウスが離されるたびに呼び出されるはずのイベントを作成しました。このイベントは、非常に単純なアクションを行います - それは、ユーザが強調されたものを、テキストを見つけるためにwindow.getSelectionを呼び出し、それは警告でそれを示しています。ここでjavascriptのwindow.getSelection()関数は信頼性がありませんか?
は(jsfiddleでも利用可能)、時間の90%を働く私のコード、です:
function winsel() {
var sel = window.getSelection();
alert(sel);
}
$(document).ready(function() {
$("#ArticleToHighlightRFS").bind("mouseup", winsel);
})
#scrollDiv {
word-wrap: break-word;
width: 600px;
max-width: 600px;
min-width: 600px;
height: 700px;
max-height: 700px;
min-height: 700px;
overflow-y: scroll;
border: 2px solid red;
margin-left: 10px;
margin-right: 10px;
margin-bottom: 10px;
padding-left: 10px;
padding-right: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<center>
<div id="scrollDiv">
<article id="ArticleToHighlightRFS">
While awaiting execution, Bonhoeffer recorded a number of his thoughts in a work we now know as Letters and Papers
from Prison. One of these essays, entitled On Stupidity, records some of the problems which Bonhoeffer likely saw at work
in Hitler’s rise to power:
“Upon closer observation, it becomes apparent that every strong upsurge of power in the public sphere, be it of a political
or a religious nature, infects a large part of humankind with stupidity. … The power of the one needs the stupidity of the other.
The process at work here is not that particular human capacities, for instance, the intellect, suddenly atrophy or fail. Instead,
it seems that under the overwhelming impact of rising power, humans are deprived of their inner independence and, more or less consciously,
give up establishing an autonomous position toward the emerging circumstances. The fact that the stupid person is often stubborn must not
blind us to the fact that he is not independent. In conversation with him, one virtually feels that one is dealing not at all with him as a
person, but with slogans, catchwords, and the like that have taken possession of him. He is under a spell, blinded, misused, and abused
in his very being. Having thus become a mindless tool, the stupid person will also be capable of any evil and at the same time incapable of seeing that it is evil. This is where the danger of diabolical misuse lurks, for it is this that can once and for all destroy human beings.”
</article>
</div>
</center>
動作しません数回の質問の原因です。 jsfiddleページに移動してコードを実行すると、テキストが入ったボックスが表示されます。最初の単語は "While"です。 「e」の後ろの空白から始め、すべての方法を逆方向にボックスの境界に合わせて強調します。私が運が良ければ、アラートボックスにWHILEという単語が表示されます。私が不運な時、何も起こらない。エラーが報告されたかどうかを確認するためにJavaScriptコンソールを試しましたが、報告されていませんでした。
あなたが問題を再現したい場合は、この操作を行います。
ハイライト「しばらく」と警戒を待ちます。
は、他のいくつかの単語をハイライト表示し、好ましくは、行の途中で、アラートを待ちます。
ハイライト再び "しばらく"。 うまくいけば、これを何度か実行すると、問題が表示されます。「while」の警告は表示されません。
(私はWindows 10でChromeを使用しています)。
この問題は再現できません。 –
スクリプトが正しく動作しているように見えます。何も起こらない唯一の「状況」は、テキストを囲むボックスの外側でマウスを「離してください」テキスト内でクリックを開始する、テキスト領域の外に出す – Bonatti