div 添加到 DOM 之前的 Sitecore 页面编辑器 运行 脚本

Sitecore Page Editor running script before div added to DOM

昨天我问了 关于我在尝试通过页面编辑器添加渲染后没有显示的问题,即使通过内容编辑器上的演示详细信息添加它时效果很好.渲染中包含一个 JavaScript 函数,它似乎是罪魁祸首,特别是因为如果删除它,渲染将工作得很好。

@MarekMusielak 给了我一个很好的问题答案,但有以下内容要说:

The issue is that when you add a component through Page Editor, the script is fired before the div element is added to DOM. Don't ask me why...

我一直在努力弄清楚为什么会发生这种情况,但我似乎无法在网上找到任何相关信息,而且 none 我的同事也知道为什么会发生这种情况。这真的开始困扰我了。

为什么页面编辑器 <div> 添加到 DOM 之前触发脚本?

我绝不认为自己是现代浏览器 DOM 方面的专家,但我确实找到了这个 - 可能值得一试。

Colin Ihrig writes:

As previously stated, tags cause the browser to block the rest of the page while the script is processed. This can lead to problems if the script references DOM elements which haven’t finished loading yet. In this scenario, the DOM-related code is typically placed in an event handler such as window load or DOMContentLoaded. Another option is to postpone execution until the document has been parsed using the “defer” attribute.

他的整篇文章都很透彻。我会尝试弄乱 "defer" 和 "async",看看它是否对您的体验有任何影响。