CreateTextRange is not working in Chrome

我的梦境 提交于 2019-11-28 09:10:44
GôTô

CreateTextRange is a Microsoft specific function, but there is an easy work around.

Use createRange instead as in this post for example:

if (document.selection) { //IE
    var range = document.body.createTextRange();
    range.moveToElementText(document.getElementById(containerid));
    range.select();
} else if (window.getSelection) { //others
    var range = document.createRange();
    range.selectNode(document.getElementById(containerid));
    window.getSelection().addRange(range);
}

createTextRange is only in IE.

Have a look at this one http://help.dottoro.com/ljrvjsfe.php

I had this issue with node's JSDOM and codemirror (which attempts to use document.createRange)

It happens because document.createRange (chrome) does not exist ATM on JSDOM and so it tries to use document.body.createTextRange (IE) instead and falls over.

To fix this I had to stub the document.createRange function in my unit test setup as follows:

global.document.createRange = () => {
  return {
    setEnd: () => {},
    setStart: () => {},
    getBoundingClientRect: () => {}
  }
}

There is talk about JSDOM polyfilling document.createRange:

See https://github.com/tmpvar/jsdom/issues/399

At the time of writing this has not yet happened.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!