gpt4 book ai didi

javascript - CreateContextualFragment 在 Safari 浏览器中不起作用

转载 作者:行者123 更新时间:2023-11-29 14:42:51 24 4
gpt4 key购买 nike

我们在 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 Exception 9: The implementation did not support the requested type of object or operation.

最佳答案

我意识到我的回答有点晚了,但我最近遇到了类似的情况。

我发现了什么

根据 https://developer.mozilla.org/en-US/docs/Web/API/Range/createContextualFragment#Browser_compatibilitySafari 9.0 或 9.1 不支持 Range.createContextualFragment()。不过它在 Safari 10 中运行良好。

你能做什么?

由于 Safari 不支持 createContextualFragment,我们可以将创建文档片段的责任委托(delegate)给 jQuery。下面说明了根据您使用的 jQuery 版本执行此操作的两个选项:

  1. jQuery 1.8 或更新版本 使用 ParseHTML
    document.getElementsByTagName('body')[0].insertBefore($.parseHTML("<div></div><script src='http://google.ca'></script>"), null);

  2. 否则就让 jQuery 自己想办法
    document.getElementsByTagName('body')[0].insertBefore($("<div></div>
    <script src='http://google.ca'></script>"), null);

关于javascript - CreateContextualFragment 在 Safari 浏览器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36426594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com