2
function getSelectionHtml() {
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
return html;
}
私はここに提供されている上記のコードを使用しています:How to get selected html text with javascript?。HTML選択の取得
しかし、関数によって返される実際のhtmlとhtmlには矛盾があります。私は
x jumps over the lazy <a href="http://www.google.com">dog</a></p>
<li>
<ul>1</ul>
<ul>2</ul>
<ul>3</ul>
機能を選択することだった場合は、実際に私が前に余分なタグが表示されていることに気付きました
<div><p>x jumps over the lazy <a href="http://www.google.com">dog</a></p>
<li>
<ul>1</ul>
<ul>2</ul>
<ul>3</ul>
<ul>4</ul>
<ul>5</ul>
</li>
</div>
を返し
<div>
<p><b>The quick brown fox jumps over the lazy <a href="www.google.com">dog</a></b></p>
<img src="http://www.fakingnews.firstpost.com/wp-content/uploads/2015/12/kim-jaya.jpg" alt="Smiley face" height="42" width="42">
<hr>
<p>The quick brown fox jumps over the lazy <a href="http://www.google.com">dog</a></p>
<li>
<ul>1</ul>
<ul>2</ul>
<ul>3</ul>
<ul>4</ul>
<ul>5</ul>
</li>
</div>
<br />
<input id="showSelected" type="button" value="showSelected" />
:たとえば、次のHTMLを与えられましたリストを選択しても、矛盾のインスタンスがあると確信しています。正確なHTMLコピーを取得するためにできることはありますか?