- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试使用范围时,我在控制台中收到此错误:
Uncaught IndexSizeError: Failed to execute 'setEnd' on 'Range': The offset 2 is larger than or equal to the node's length (0).
这是我的脚本:
<script type="text/javascript">
function gotMark() {
var range = document.createRange();
var startNode = document.getElementById("dividTeste1").firstChild;
range.setStart(startNode, 0);
range.setEnd(startNode, 2);
var newNode = document.createElement("span");
range.surroundContents(newNode);
}
</script>
和页面:
<div class="ui-block-a" id="divid" value="div1">
<div style="background-color: black; color: white; padding: 20px;" id="divid2">
<h2>London</h2>
<p id="dividTeste1">London is the capital city of England. It is the most
populous city in the United Kingdom, with a metropolitan area of
over 13 million inhabitants.</p>
</div>
<div style="background-color: black; color: white; padding: 20px;" id="divid2">
<h2>London</h2>
<p id="dividTeste2">London is the capital city of England. It is the most
populous city in the United Kingdom, with a metropolitan area of
over 13 million inhabitants.</p>
</div>
</div>
<input type="button" value="Marcar com range" onClick="gotMark()"
id="marcarconranger12" />
我尝试按照
dom range.setStart / setEnd 中的说明进行操作选择开始和结束位置。
最佳答案
虽然 OP 的代码似乎是安全且有界限的,但在同一节点上进行多次操作时经常会出现问题,这会改变其特性并抛出您可能进行的后续偏移计算。
例如,假设我们想在一个开始-结束对数组的段落中添加高亮。天真的方法是在对上向前迭代并为每个开始-结束范围执行插入:
const highlightPositions = (textEl, positions) =>
positions.forEach(([start, end]) => {
const range = document.createRange();
range.setStart(textEl.firstChild, start);
range.setEnd(textEl.firstChild, end);
const span = document.createElement("span");
span.style.background = "#0f6";
range.surroundContents(span);
})
;
const positions = [[0, 3], [8, 11]];
highlightPositions(document.querySelector("p"), positions);
<p>foo bar baz</p>
const highlightPositions = (textEl, positions) =>
positions.slice().reverse().forEach(([start, end]) => {
const range = document.createRange();
range.setStart(textEl.firstChild, start);
range.setEnd(textEl.firstChild, end);
const span = document.createElement("span");
span.style.background = "#0f6";
range.surroundContents(span);
})
;
const positions = [[0, 3], [8, 11]];
highlightPositions(document.querySelector("p"), positions);
<p>foo bar baz</p>
Node.normalize()
将子文本粘合在一起,在新节点或字符串上构建所需文本并设置
.innerHTML
当它准备好时,等等。
关于javascript - 尝试在范围 : Uncaught IndexSizeError: Failed to execute 'setEnd' on 'Range' 上使用 setStart 和 setEnd 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29831188/
我正在尝试更改音频的音量,但在尝试时,我在控制台日志中收到 IndexSizeError: Index or size is negative or more than the allowed amo
我正在使用 ChartJS 显示不同项目的饼图和条形图。当我单击“统计信息”选项卡中的第一个项目时,它会正确显示图表。当我点击下一个项目时,它不显示从那里开始的图表。相反,抛出一个异常: Uncaug
我正在查询“keyup”上的 websql 表,因此它返回所查询字符串的前 10 个匹配项的列表,我将此搜索字符串保存在一个名为 currval 的变量中,我将其传递给查询字符串。 当我的结果集等于
我将标准 iframe 嵌入到显示 SoundCloud 音乐播放器的非 SSL 网页中。播放器加载正常,但我在 Safari 开发者工具中发现错误如下: IndexSizeError: DOM Ex
当尝试使用以下代码清除新内容并将其加载到 tinyMce 时 tinymce.activeEditor.setContent('') 它在 中显示错误 IE9, IE10 显示 IndexSizeEr
我从 收到此错误哨兵在 ios 中重复 - safari 14.0 在 react 项目中。 没有代码跟踪,也没有其他信息,但它几乎在每个 URL 中重复。 我已经到处寻找选项。我试过调试但我无法复制
我正在尝试检测图像中的面部。我只需要用户选择的图像中的面孔数量。我使用以下代码将图像从输入字段加载到预览 div 中: function readURL(input) { if (input.
我正在使用 html2canvas 转换 Canvas 上的 div。像这样: ... ... $('#demo').html2canvas({ onrendered:
我发现某些代码在 Internet Explorer 和 Edge 中失败,但在 Chrome 和 Firefox 中似乎可以完美运行: var image = document.getElement
因此,在 Firefox 中,当我在 Canvas 元素上使用 drawImage 时,我在控制台中收到此错误。 "IndexSizeError: Index or size is negative
我正在使用 jquery 和谷歌地图 v3 创建一个页面,并且我正在尝试在谷歌地图上显示一个标记。出于某种原因,我在使用 Firefox 时遇到错误(并且标记未显示在 map 上): Error: I
clear_table = function( ) { var table
我正在使用 Chart.js Arc 函数绘制圆环图。有一段时间效果很好。但最近它开始抛出上述异常。 Chart.js Arc 函数中的以下行抛出异常: ctx.arc(this.x, this.y,
设置blurDelay = 0后,点击Bold,点击Paste in toolbar,会报如下错误。 未捕获的 IndexSizeError:无法在“选择”上执行“扩展”:1 大于给定节点的长度。 C
当我尝试使用范围时,我在控制台中收到此错误: Uncaught IndexSizeError: Failed to execute 'setEnd' on 'Range': The offset 2
我以前使用 Chrome,但我需要使用 Firefox 测试我的插件。我正在 try catch 视频元素并将其绘制到 Canvas 上。在 Chrome 中,一切正常,但在 Firefox 中,我遇
为什么图片没有出现在我的页面上?未捕获的 IndexSizeError:无法在“HTMLTableRowElement”上执行“insertCell”:提供的值 (1) 超出范围 [-1, 0] -
我是一名优秀的程序员,十分优秀!