Encoded HTML in optionsText binding of knockout js

南楼画角 提交于 2019-12-24 14:18:14

问题


I am trying to create a dropdown list with html encoded options in knockout js using options binding.

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>

In general, I need a function that take a encoded html string and returns the decoded text.

i.e. the function takes something like

blah blah balh <sup>TM</sup>

and return

blah blah blah ™


回答1:


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:

  • 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)


来源:https://stackoverflow.com/questions/29942763/encoded-html-in-optionstext-binding-of-knockout-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!