admin管理员组文章数量:1323224
I need to surround multiple words with spans, I know startIndex and endIndex for each word (I'm sure no word will be spanning in multiple tags and all the words are in the same element) I can't even select the first word, I get "IndexSizeError: Index or size is negative or greater than the allowed amount" and similar error with rangy ( / ) what am I doing wrong?
var range = document.createRange();
startNode = document.getElementById("texttocheck");
range.setStart(startNode, 0);
range.setEnd(startNode, 4);
var newNode = document.createElement("span");
range.surroundContents(newNode);
here is the fiddle: /
I need to surround multiple words with spans, I know startIndex and endIndex for each word (I'm sure no word will be spanning in multiple tags and all the words are in the same element) I can't even select the first word, I get "IndexSizeError: Index or size is negative or greater than the allowed amount" and similar error with rangy ( http://jsfiddle/pastrocchio/hgugQ/7/ ) what am I doing wrong?
var range = document.createRange();
startNode = document.getElementById("texttocheck");
range.setStart(startNode, 0);
range.setEnd(startNode, 4);
var newNode = document.createElement("span");
range.surroundContents(newNode);
here is the fiddle: http://jsfiddle/pastrocchio/hgugQ/3/
Share Improve this question edited Apr 22, 2013 at 10:53 chickpeas asked Apr 22, 2013 at 10:45 chickpeaschickpeas 4435 silver badges16 bronze badges 2- Are you only trying to select the first word or all of them? Do you have to use createRange() or can you use a different approach? – Derek Henderson Commented Apr 22, 2013 at 11:18
- some of them: the wrong ones, it will be a spellcheck – chickpeas Commented Apr 22, 2013 at 11:27
1 Answer
Reset to default 11I figured it out, I was missing startnode.firstChild
var range = document.createRange();
startNode = document.getElementById("texttocheck");
range.setStart(startNode.firstChild, 0);
range.setEnd(startNode.firstChild, 4);
var newNode = document.createElement("span");
range.surroundContents(newNode);
本文标签: javascriptCan39t surroundContents with range setStart setsetEndStack Overflow
版权声明:本文标题:javascript - Can't surroundContents with range set.Start set.setEnd - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742072654a2419221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论