options中的编码HTML敲除js的文本绑定 [英] Encoded HTML in optionsText binding of knockout js
问题描述
我正在尝试使用选项绑定在击倒js中创建带有html编码选项的下拉列表.
I am trying to create a dropdown list with html encoded options in knockout js using options binding.
如何解码从绑定到optionsText绑定的函数返回的文本.
How can i decode the text returned from the function binded to the optionsText binding.
<select data-bind="options: items, optionsText: function(item){ return "decoded text"}"></select>
通常,我需要一个函数,该函数采用编码的html字符串并返回解码的文本.
In general, I need a function that take a encoded html string and returns the decoded text.
即该功能需要类似
blah blah balh <sup>TM</sup>
并返回
等等等等
推荐答案
这将是不可能的. option
标签不允许包含其他标签作为内容,仅"普通字符数据".这确实意味着您可以使用实体,例如™
或™
(呈现为™"),它们应适用于您的特定示例.
This will not be possible. An option
tag is not permitted to have other tags as content, only "Normal character data". This does mean you can use entities, e.g. ™
or ™
(which renders as "™"), which should work for your specific example.
出于完整性考虑,对于淘汰赛"部分,如果您要尝试尝试以下操作,则有两种选择:
For completeness sake, for the Knockout part, if you were to try what you're after, you have two options:
- 利用
foreach
绑定并创建具有html
绑定内容(而不是text
绑定内容)的自己的option
元素 - 利用
optionsAfterRender
绑定来调整呈现的选项元素(感谢@CaseyWebb在注释中注意到此选项)
- utilize the
foreach
binding and create your ownoption
elements withhtml
bound contents (as opposed totext
bound contents) - utilize the
optionsAfterRender
binding to tweak the rendered option elements (thanks to @CaseyWebb for noting this option in the comments)
这篇关于options中的编码HTML敲除js的文本绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!