通过忽略div标签javascript的内部元素来选择文本
<html>
<body>
<script language="javascript">
function getSelectionHTML()
{
var div = document.getElementById("myDiv");
if (document.createRange) {
var textNode=div.firstChild;
var rangeObj=document.createRange();
rangeObj.setStart(textNode,0);
rangeObj.setEnd(textNode,5);
div .innerHTML = div .innerHTML.replace(rangeObj.toString(), '<span style="background-color: lime">'+rangeObj.toString()+'</span>')
}
}
</script>
<div id="myDiv">
asdf as<b>dfas df asf asdf sdfjk dvh a sjkh jhcdjkv</b> iof scjahjkv ahsjv hdjk biud fcsvjksdhf k
</div>
<form name="aform">
<input type="button" value="Get selection" onclick="getSelectionHTML()">
</body>
</html>
好的。让我解释一下-> getSelectionHTML() 方法用于选择从 0 到 10 的字符。我通过“myDiv”id 获取值。但内部粗体、斜体和其他标签给我带来了麻烦。
简而言之,我只想选择“myDiv”标签中的前十个字符(并应用它们跨度标签)。
我到底缺少什么?
<html>
<body>
<script language="javascript">
function getSelectionHTML()
{
var div = document.getElementById("myDiv");
if (document.createRange) {
var textNode=div.firstChild;
var rangeObj=document.createRange();
rangeObj.setStart(textNode,0);
rangeObj.setEnd(textNode,5);
div .innerHTML = div .innerHTML.replace(rangeObj.toString(), '<span style="background-color: lime">'+rangeObj.toString()+'</span>')
}
}
</script>
<div id="myDiv">
asdf as<b>dfas df asf asdf sdfjk dvh a sjkh jhcdjkv</b> iof scjahjkv ahsjv hdjk biud fcsvjksdhf k
</div>
<form name="aform">
<input type="button" value="Get selection" onclick="getSelectionHTML()">
</body>
</html>
Ok. Let me explain -> getSelectionHTML() method is for selection of characters from 0 to 10. I am getting the values by "myDiv" id. but inner bold, italic & other tags are giving me trouble.
In simple words, I just want to make selection of first ten characters (& apply them span tag) which are in "myDiv" tag.
What exactly I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 IE 中使用 TextRange 会更容易,它基于字符、单词和句子,而不是节点和偏移量。在非 IE 浏览器中,您需要对 DOM 中的文本节点进行繁琐的手动遍历。但是,即使您这样做是为了获取
中的前十个文本字符(即“asdf asdfa”),您的策略也是有缺陷的,因为您正在使用替换在
innerHTML
上,它会尝试找到“asdf asdfa”,但失败(innerHTML
将以“asdf asdfa”开头,或者可能是“asdf as
”,具体取决于浏览器)。
我建议进行 DOM 遍历,以查找
内的所有文本节点,并使用
包围所需的文本节点部分,从而完全避免了范围,从而使其在所有主要浏览器中都可以工作。
This is much easier in IE using
TextRange
, which is based around characters, words and sentences rather than nodes and offsets. In non-IE browsers, you'll need to do tedious manual traversal of text nodes in the DOM. However, even if you were to do this to get the first ten text characters within your<div>
(which would be "asdf asdfa"), your strategy is flawed because you're using a replacement oninnerHTML
, which would try and fail to find "asdf asdfa" (innerHTML
would start with "asdf as<b>
dfa", or possibly "asdf as<B>
", depending on browser).What I would suggest is doing the DOM traversal to find all text nodes within the
<div>
and surrounding the parts of the text nodes you need with<span>
s, thus avoiding Ranges altogether and therefore making this work in all major browsers.您正在尝试选择文本的第 1 到 10 个字符。但是当使用
Range.setStart
和.endStart
,第一个参数是包含文本的文本节点。如果您使用 Firebug(或 Web Inspector)浏览 DOM,您会注意到文本的字符 10 驻留在另一个元素(元素)中,并具有自己的文本节点。
顺便说一句,您遗漏了几个必需的元素/标签,它们也可能成为错误来源。
我的更正版本显示
现在
rangeObj
包含所选文本,但您不能简单地按照您尝试的方式插入元素,因为元素无法嵌套这边走:
You're trying to select e.g. character 1 to 10 of your text. But when using
Range.setStart
and.endStart
, the first parameter is the text node containing your text. If you browse through the DOM with Firebug (or Web Inspector), you'll notice that character 10 of your text resides in another element (the<b>
element), with its own text node.BTW, you left out several required elements/tags, which can also be a source of errors.
My corrected version reads
Now
rangeObj
contains the selected text, but you can't simply insert a<span>
element the way you tried, because elements can't be nested this way: