html - 我应该手动将自定义 knockout 元素注册到 DOM 吗?

标签 html dom knockout.js custom-element

在 knockout 中我们可以创建 custom elements看起来像这样:

<flight-deals params='from: "lhr", to: "sfo"'></flight-deals>

HTML中自定义元素的定义仍然是work in progress以及using this today的部分流程是使用 document.registerElement 将自定义元素注册到 DOM。

但是,我在 knockout 文档中找不到有关这些方面的任何内容,当我调查我的 custom elements are registered 时,我找不到任何内容。调用ko.components.register后通过knockout到DOM,结果不是。

因此,如果我在 knockout 中使用自定义元素,我是否还应该确保使用 document.registerElement 手动注册这些元素?事实上, knockout 还没有做到这一点,这让我有点困惑。

最佳答案

您不需要为现代浏览器和 IE9+ 做任何特殊的事情。

对于 IE6 - IE8 支持,您确实需要意识到这一点并使用一点点魔法。如the relevant documentation提及:

  • HTML5-era browsers, which includes Internet Explorer 9 and later, automatically allow for custom elements with no difficulties.
  • Internet Explorer 6 to 8 also supports custom elements, but only if they are registered before the HTML parser encounters any of those elements.

IE 6-8’s HTML parser will discard any unrecognized elements. To ensure it doesn’t throw out your custom elements, you must do one of the following:

  • Ensure you call ko.components.register('your-component') before the HTML parser sees any <your-component> elements
  • Or, at least call document.createElement('your-component') before the HTML parser sees any <your-component> elements. You can ignore the result of the createElement call — all that matters is that you have called it.

关于html - 我应该手动将自定义 knockout 元素注册到 DOM 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35036700/

相关文章:

javascript - 在 DOM 模型/JavaScript 中,文本框和输入的值之间有区别吗?

css - 将单元格内容转为可编辑的输入框

jquery - 选中单选按钮时突出显示按钮

html - 为什么这段创建存档页面的代码在这个博客上不起作用?

javascript - 尝试使用php解析网页

javascript - 如何解析C :\fakepath?

knockout.js - 无法解析绑定(bind)。 knockout 错误

javascript - 使用 knockoutjs 绑定(bind)和显示字典

javascript - 单击选项卡更改标题的内容

html - Emmet 缩写语法乘法将所有元素放在同一行而不是多个?