options中的编码HTML敲除js的文本绑定 [英] Encoded HTML in optionsText binding of knockout js

查看:86
本文介绍了options中的编码HTML敲除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标签不允许包含其他标签作为内容,仅"普通字符数据".这确实意味着您可以使用实体,例如&#8482;&trade;(呈现为™"),它们应适用于您的特定示例.

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. &#8482; or &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:

  • 利用foreach绑定并创建具有html绑定内容(而不是text绑定内容)的自己的option元素
  • 利用optionsAfterRender绑定来调整呈现的选项元素(感谢@CaseyWebb在注释中注意到此选项)
  • utilize the foreach binding and create your own option elements with html bound contents (as opposed to text 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆