最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Failed to execute 'getRangeAt' on 'Selection': 0 is not a valid index - Stack Overf

programmeradmin1浏览0评论

I have this problem with Chrome and Safari (works in Mozilla and IE + Edge). I want to store the caret position and selection using range=sel.getRangeAt(0);

However, I get the following error in Chrome: Uncaught IndexSizeError: Failed to execute 'getRangeAt' on 'Selection': 0 is not a valid index.

How can I fix this in another way than the following: here.

This is my fiddle

The reason why I can't use this method is because I display a hidden div, and insert text into an <input type="text"> field, I then insert that into a contenteditable div. Selecting this input field makes me save a new selection, and messes up the insert.

Here are some images to display the problem:

Part 1: The contenteditable div fields Part 2: The hidden div that is displayed, and typing, changing the carter position Part 3: On insert, console error in Chrome and Safari

Any suggestions?

I have this problem with Chrome and Safari (works in Mozilla and IE + Edge). I want to store the caret position and selection using range=sel.getRangeAt(0);

However, I get the following error in Chrome: Uncaught IndexSizeError: Failed to execute 'getRangeAt' on 'Selection': 0 is not a valid index.

How can I fix this in another way than the following: here.

This is my fiddle

The reason why I can't use this method is because I display a hidden div, and insert text into an <input type="text"> field, I then insert that into a contenteditable div. Selecting this input field makes me save a new selection, and messes up the insert.

Here are some images to display the problem:

Part 1: The contenteditable div fields Part 2: The hidden div that is displayed, and typing, changing the carter position Part 3: On insert, console error in Chrome and Safari

Any suggestions?

Share Improve this question edited May 23, 2017 at 10:30 CommunityBot 11 silver badge asked Jan 14, 2016 at 12:27 GjertGjert 1,0672 gold badges19 silver badges50 bronze badges 2
  • Would you be able to provide jsfiddle example with this issue? – Arturas Tamulaitis Commented Jan 26, 2016 at 8:56
  • 1 See provided fix of similar problem. Exactly the same issue. Only problem is that he solves the problem for a <option> menu. While I change my carter position in a type="text". – Gjert Commented Jan 26, 2016 at 9:25
Add a ment  | 

1 Answer 1

Reset to default 5 +50

Ok so i`ve found a mistake in your code, and it was pretty simple, though hard to find :)

Issue:

Line 46/72: bE1.focus() does not work therefore object Selection.range does not exist. I`ve did simple check by calling console.log(document.activeElement) to check which element is active in page, and that was wrong element.

Solution:

Changed bE1.focus() --> div_new_1.focus()

Working example: jsFiddle

Functions i`ve changed:

function lrestoreRangePosition1() {
  closePopupLink1();
  div_new_1.focus();
  var sel = window.getSelection(),
    range = sel.getRangeAt(0);
  var x, C, sC = bE1,
    eC = bE1;

  C = rp.sC;
  x = C.length;
  while (x--) sC = sC.childNodes[C[x]];
  C = rp.eC;
  x = C.length;
  while (x--) eC = eC.childNodes[C[x]];

  range.setStart(sC, rp.sO);
  range.setEnd(eC, rp.eO);
  sel.removeAllRanges();
  sel.addRange(range)
}

function lrestoreRangePositionInsert1() {
  var newText1 = document.getElementById('url_title1').value;
  var newText2 = document.getElementById('url_content1').value;

  var pasteText = "<a href='" + newText2 + "'>" + newText1 + "</a>";
  closePopupLink1();

  div_new_1.focus();
  console.log(document.activeElement)
  var sel = window.getSelection(),
    range = sel.getRangeAt(0);
  var x, C, sC = bE1,
    eC = bE1;

  C = rp.sC;
  x = C.length;
  while (x--) sC = sC.childNodes[C[x]];
  C = rp.eC;
  x = C.length;
  while (x--) eC = eC.childNodes[C[x]];

  range.setStart(sC, rp.sO);
  range.setEnd(eC, rp.eO);
  sel.removeAllRanges();
  sel.addRange(range);

  pasteHtmlAtCaret(pasteText);
}

Let me know if that helps:)

发布评论

评论列表(0)

  1. 暂无评论