How to convert a string into a HTML element in Mithril?

笑着哭i 提交于 2020-01-21 12:06:27

问题


Suppose I have a string <span class="msg">Text goes here</span>.I need to use this string as a HTML element in my webpage. Any ideas on how to do it?


回答1:


Mithril provides the m.trust method for this. At the place in your view where you want the HTML output, write m.trust( '<span class="msg">Text goes here</span>' ) and you should be sorted.




回答2:


Mithril it's powerfull thanks to the virtual dom, in the view you if you want to create a html element you use:

m("htmlattribute.classeCss" , "value");

So in your case:

m("span.msg" , "Text goes here");



回答3:


Try creating a container you wish to hold your span in.
1. Use jQuery to select it.
2. On that selection, call the jQuery .html() method, and pass in your HTML string.
($('.container').html(//string-goes-here), for example)

You should be able to assign the inner HTML of the container with the string, resulting in the HTML element you want.

Docs here.



来源:https://stackoverflow.com/questions/30435250/how-to-convert-a-string-into-a-html-element-in-mithril

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