私はHtmlエディタを持っており、選択したテキストが親を満たしているか親が他のコンテンツを持っているかどうかを知りたい。選択したテキストが親を埋めるかどうかを知る方法?
<div>Hi this is <span>balh balh</span></div>
は、ユーザーがblah blah
またはHi this is balh balh
フレーズを選択true
にする必要がありますチェックして、ユーザーがis blah blah
フレーズを選択すると、その後false
でなければならないチェック:エディタツールで作成されたこのHTMLを想定 。 私は以下のコードを書いています。
$('#getSelection').click(function(){
var father = $(window.getSelection().focusNode.parentNode);
var hasFather = father[0].innerText === window.getSelection().toString();
alert(hasFather);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Hi this is <span>balh balh</span></div>
<input type="button" id="getSelection" value="Get Selection"/>
しかし、生成されたHTMLが複雑に得るとき、それは働いていません。あなたの親ノードが変更された
$('#getSelection').click(function(){
var father = $(window.getSelection().focusNode.parentNode);
var hasFather = father[0].innerText === window.getSelection().toString();
alert(hasFather);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="editable" contenteditable="TRUE" style="padding: 0px; color: rgb(0, 0, 0); font-family: Yekan; font-size: 12px; line-height: 20px; text-align: center; outline: -webkit-focus-ring-color auto 5px; border-color: rgb(204, 204, 204);"><div style="text-align: center;"><span style="font-style: italic; font-weight: bold; text-decoration: underline;"></span><span style="font-size: 13px;"><span style="font-size: 14px;"><span style="font-style: italic; font-weight: bold; text-decoration: underline;">blah blah blah blah blah </span><span style="font-style: italic; font-weight: bold;">blah </span><span style="font-style: italic; font-weight: bold; text-decoration: underline;">blah blah blah </span><span style="font-style: italic; font-weight: bold;">blah blah blah blah blah</span><span style="font-style: italic; font-weight: bold; text-decoration: underline;"> blah blah blah blah</span></span></span><span style="font-style: italic; font-weight: bold; text-decoration: underline;"></span></div></div>
<input type="button" id="getSelection" value="Get Selection"/>
あなたは下線タグの内容を選択した場合、あなたはあなたの答えは動作しませ参照してください。 –
はい、これを見てください。あなたの初期ケースのOR条件を追加するのを忘れました。私は私の答えを編集しました、今働くべきです。 –