CreateContextualFragment在safari中不起作用
CreateContextualFragment not working in safari
我们在safari中的函数(createContextualFragment)
有问题。
我们需要在正文中添加一些内容,所以我们使用这行代码。
代码:
document.getElementsByTagName('body')[0].insertBefore(document.createRange().createContextualFragment("<div></div><script src="LINK_SOURCE"></script>"), null);
这行代码在Chrome和Firefox上运行良好,但我们在Safari中的createContextualFragment遇到了一些问题。
Safari中的错误:
createContextualFragment--asyncronousDocumentWrite2.html:28:115NotSupportedError:DOM异常9:实现不支持请求的对象类型或活动
我意识到我的答案来得有点晚,但我最近遇到了类似的情况。
我发现了什么
根据https://developer.mozilla.org/en-US/docs/Web/API/Range/createContextualFragment#Browser_compatibilitySafari 9.0或9.1不支持Range.createContextualFragment()。不过,它在Safari 10中运行良好。
你能做什么
由于Safari不支持createContextualFragment,我们可以将创建文档片段的责任委托给jQuery。以下说明了根据您使用的jQuery版本来执行此操作的两个选项:
-
jQuery 1.8或更新版本使用ParseHTML
document.getElementsByTagName('body')[0].insertBefore($.parseHTML("<div></div><script src='http://google.ca'></script>"), null);
-
否则让jQuery来决定该怎么做
document.getElementsByTagName('body')[0].insertBefore($("<div></div> <script src='http://google.ca'></script>"), null);
我发现设置range.selectNodeContents(document.createElement('div'));
可以修复本文间接指出的问题https://grrr.tech/posts/create-dom-node-from-html-string/#range.
我的示例用法:
document.querySelectorAll('.container').forEach((el) => {
const range = document.createRange();
range.selectNodeContents(el);
range.deleteContents();
range.selectNodeContents(document.createElement('div')); // fix for safari
el.appendChild(range.createContextualFragment(htmlContent));
});
- window.open(url).print()在Safari中不起作用
- 复制到剪贴板在safari-angularjs中不起作用
- Javascript Cookies在Safari中不起作用
- 带有Safari的Javascript;不起作用——所有其他浏览器都起作用
- 导航到新url时,Javascript在Safari中不起作用
- JS代码使IE9和Safari冻结,在Opera中不起作用
- html视频javascript播放方法在移动Safari中不起作用
- 为什么这个排序功能在 Safari 中不起作用
- $.getJSON 在 Chrome 和 Safari 中不起作用
- window.dispatchEvent 在 Firefox、Safari 或 IE 中不起作用
- 滚动到顶部按钮在 chrome 或 safari 中不起作用
- jQuery .play() 在移动 Safari 上不起作用
- jQuery .focus() 在 Safari(桌面)的搜索字段中不起作用
- HTML5<视频>元素在Firefox、Opera和Safari中不起作用
- 为什么find('input:focus')在safari中不起作用
- OfflineAudioContext.startRendering()基于承诺的函数在safari中不起作用
- ng模式验证在safari中不起作用
- 图像预览在Safari中不起作用
- 脚本在safari中不起作用
- webcomponents.js polyfills 不起作用:Safari 和 Firefox