从 Javascript 绑定到 MetroStyle App 中的 InnerHTML

Binding to InnerHTML in MetroStyle App from Javascript

本文关键字:App 中的 InnerHTML MetroStyle Javascript 绑定      更新时间:2023-09-26

>我有一个 Div 定义为:

<div class="article-content" data-win-bind="innerHTML: content"></div>

并使用具有内容属性的对象绑定到它。

现在我使用外部资源创建的对象,有时 content 属性将具有这样的内容:

"some text <iframe width="560" height="315" src="youtubelink" frameborder="0" allowfullscreen></iframe>"

此抛出异常:

Unable to add dynamic content. A script attempted to inject dynamic content, or elements previously modified dynamically, that might be unsafe. For example, using the innerHTML property to add script or malformed HTML will generate this exception. Use the toStaticHTML method to filter dynamic content, or explicitly create elements and attributes with a method such as createElement. 

了解异常以及它发生的原因,但现在我该如何使这项工作?

查看本文档的"动态添加 HTML"部分 您可能需要使用 execUnsafeLocalFunction 来注入您尝试注入的特定 HTML。 另外,请查看这篇博客文章,了解如何创建可在 Metro 风格应用中工作的通用追加函数。