2016-07-07 5 views
0

記事タグにテキストを挿入します。記事タグは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コンソールを試しましたが、報告されていませんでした。
あなたが問題を再現したい場合は、この操作を行います。

  1. ハイライト「しばらく」と警戒を待ちます。

  2. は、他のいくつかの単語をハイライト表示し、好ましくは、行の途中で、アラートを待ちます。

  3. ハイライト再び "しばらく"。 うまくいけば、これを何度か実行すると、問題が表示されます。「while」の警告は表示されません。

(私はWindows 10でChromeを使用しています)。

+0

この問題は再現できません。 –

+0

スクリプトが正しく動作しているように見えます。何も起こらない唯一の「状況」は、テキストを囲むボックスの外側でマウスを「離してください」テキスト内でクリックを開始する、テキスト領域の外に出す – Bonatti

答えて

0

答えは、少なくともあなたのポストの文脈では、ありません可能性があります。いくつかのJSメソッドは、複数のブラウザ/デバイス/バージョン間で信頼性がありません(異なる実装の詳細を行う)、まったく同じブラウザ/デバイス/バージョン内で信頼性の低いメソッドはほとんどありません。

この問題はおそらくあなたのコードの他の領域に存在する可能性があり、この状況では非常に簡単に証明できます。alertメソッドは「偽」値に問題はなく、 window.getSelectionの出力は有効ではありません。唯一の方法は、window.getSelection呼び出しで致命的なエラーが発生した場合です。最後に、あなたの説明によると、問題の核心は、これは言っている「を起動しません 『ながら』の警告」です。

あなたの問題を再現できず、テストで使用されている「while」という単語が#ArticleToHighlightRFS divの国境付近で疑わしいと思っていますので、上記のコメントに同意します「時々、」あなたは、従って、そのコールバックをトリガしないので、alertコールをトリガしませしたがってmouseupイベントに#ArticleToHighlightRFSをトリガーない、#ArticleToHighlightRFSのdivの外にマウスをリリースしていることを理論化。これが事実であると仮定すると、

、あなたもmouseupイベントのためbody要素を聞くことによって、問題を解決するが、唯一の希望#ArticleToHighlightRFS divの前にmousedownイベントがあった場合、それに作用することができます。私はそれが役に立てば幸い

$(document).ready(function() { 
 
    var $body = $('body') 
 
    var $articleToHighlightRFS = $('#ArticleToHighlightRFS') 
 
    var winsel = function() { 
 
     $body.off('mouseup', winsel); 
 
     var sel = window.getSelection(); 
 
     alert(sel); 
 
    } 
 
    $articleToHighlightRFS.on('mouseup', winsel); 
 
    $articleToHighlightRFS.on('mousedown', function() { 
 
    $body.one('mouseup', winsel); 
 
    }); 
 
})
body { padding: 30px; background: blue } 
 
#scrollDiv { 
 
    background: white; 
 
    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> 
 
body element is all the blue 
 
</br> 
 
</br> 
 
<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>

:インプリメンテーションは、の線に沿って何かだろう!

+0

また、周囲の要素からテキストを除外するには、[this](http://stackoverflow.com/questions/20427556/get-html-of -selection-in-a-specific-div)の回答です。 –

関連する問題