ユーザーが選択したテキストのhtmlコードを取得するための次のコードがあります。オープン<span>
タグがなくても完全に機能します。
function getHtml() {
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;
}
}
alert(html); }
<span>
ユーザーが選択したテキストのコードにOpenタグがある場合、自動的に終了</span>
タグが追加されます。この問題を解決するための解決策はありますか?
使用されるHTMLコード:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<SCRIPT LANGUAGE="JavaScript" SRC="HighlightedString.js">
</SCRIPT>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Red and Louis</title>
<link rel="stylesheet" media="screen" type="text/css" href="screen.css" />
<link rel="stylesheet" media="print" type="text/css" href="other.css" />
</head>
<body onmouseup="getHtml()">
<div id="story">
<h1>Red and Louis</h1>
<p>Once upon a time in the middle of a large forest in Western Massachusetts, there lived a little girl who was about 14 years old and who loved baseball.
<span style="font-family:Verdana, sans-serif; font-size:10pt">Text Within Span </span>
She thought that if she had grown up in the Midwest where there were hardly any trees, she would have been one of the best baseball players ever, but since she lived in the middle of a forest, there was no way for her to practice. She had tried once but every ball had headed straight for the trees and though she thought they probably would have been homeruns, she couldn’t tell for sure.</p>
</body>
</html>