如何使用 Dojo 1.10 parse/render 一个 html 字符串?
How to parse/render a html string with Dojo 1.10?
假设我有以下内容:
const props = {
buttonText : 'Click Me'
}
const htmlString = "<button>${buttonText}</button>"
dojo.someFunction(htmlString, props);
“someFunction
”的 dojo
模块等价物是什么?
我知道可以用templateMixins
,但是我不想extend/make一个widget,只能借用解析函数
您所要做的就是使用 dojo Dijit 模板中使用的 dojo/string ->
substitude 函数。
请参阅下面的工作片段:
require(["dojo/string", "dojo/dom","dojo/ready"], function(string, dom , ready) {
const props = {
buttonText: 'Click Me'
}
const htmlString = "<button>${buttonText}</button>"
var newString = string.substitute(htmlString, props);
console.log(newString);
alert("String after substute : \n"+ newString);
dom.byId("btn").innerHTML = newString ;
});
<script type="text/javascript">
dojoConfig = {
isDebug: true,
async: true,
parseOnLoad: true
}
</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<link href="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css" rel="stylesheet" />
<div id="btn"></div>
假设我有以下内容:
const props = {
buttonText : 'Click Me'
}
const htmlString = "<button>${buttonText}</button>"
dojo.someFunction(htmlString, props);
“someFunction
”的 dojo
模块等价物是什么?
我知道可以用templateMixins
,但是我不想extend/make一个widget,只能借用解析函数
您所要做的就是使用 dojo Dijit 模板中使用的 dojo/string ->
substitude 函数。
请参阅下面的工作片段:
require(["dojo/string", "dojo/dom","dojo/ready"], function(string, dom , ready) {
const props = {
buttonText: 'Click Me'
}
const htmlString = "<button>${buttonText}</button>"
var newString = string.substitute(htmlString, props);
console.log(newString);
alert("String after substute : \n"+ newString);
dom.byId("btn").innerHTML = newString ;
});
<script type="text/javascript">
dojoConfig = {
isDebug: true,
async: true,
parseOnLoad: true
}
</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<link href="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css" rel="stylesheet" />
<div id="btn"></div>