简体   繁体   English

options中的编码HTML敲除js的文本绑定

[英]Encoded HTML in optionsText binding of knockout js

I am trying to create a dropdown list with html encoded options in knockout js using options binding. 我正在尝试使用选项绑定在淘汰赛js中创建带有html编码选项的下拉列表。

How can i decode the text returned from the function binded to the optionsText binding. 我如何解码从绑定到optionsText绑定的函数返回的文本。

<select data-bind="options: items, optionsText: function(item){ return "decoded text"}"></select>

In general, I need a function that take a encoded html string and returns the decoded text. 通常,我需要一个函数,该函数接受编码的html字符串并返回解码的文本。

ie the function takes something like 即函数需要类似

blah blah balh <sup>TM</sup>

and return 然后返回

blah blah blah ™ 等等等等™

This will not be possible. 这将是不可能的。 An option tag is not permitted to have other tags as content, only " Normal character data ". 一个option标签不允许包含其他标签作为内容,只能包含“ 普通字符数据 ”。 This does mean you can use entities, eg &#8482; 这确实意味着您可以使用实体,例如&#8482; or &trade; &trade; (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: 为了完整起见,淘汰赛的一部分,如果尝试之后你在做什么,你有两个选择:

  • utilize the foreach binding and create your own option elements with html bound contents (as opposed to text bound contents) 利用foreach绑定并使用html绑定内容(而不是text绑定内容)创建自己的option元素
  • utilize the optionsAfterRender binding to tweak the rendered option elements (thanks to @CaseyWebb for noting this option in the comments) 利用optionsAfterRender绑定来调整渲染的选项元素(感谢@CaseyWebb在注释中注意到此选项)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM