Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中将字符串转换为html DOM(允许模板)_Javascript_Jquery_Html_Dom_Template Engine - Fatal编程技术网

如何在javascript中将字符串转换为html DOM(允许模板)

如何在javascript中将字符串转换为html DOM(允许模板),javascript,jquery,html,dom,template-engine,Javascript,Jquery,Html,Dom,Template Engine,我尝试转换字符串,如 <div name="{{hello}}"><br/>{{world}}</div> {{world} 在一个html dom或类似的结构中,可以让我识别所有这些模型占位符的位置 但是在尝试了 var el = document.createElement("div"); el.innerHTML = "<div name="{{hello}}"><br/>{{world}}</div>" var

我尝试转换字符串,如

<div name="{{hello}}"><br/>{{world}}</div>

{{world}
在一个html dom或类似的结构中,可以让我识别所有这些模型占位符的位置

但是在尝试了

var el = document.createElement("div");
el.innerHTML = "<div name="{{hello}}"><br/>{{world}}</div>"
var el=document.createElement(“div”);
el.innerHTML=“
{{world}”
它不起作用


是否有js库可以帮助我进行解析?

您必须采用某种模板机制。你认为“hello”的值来自哪里?除了@Pointy的注释之外,我认为你的JS的第二行中有一个错误。您可以尝试在{{hello}}:
el.innerHTML=“
{{world}}”周围使用单引号。
希望有帮助。正则表达式或简单的字符串替换解决方案怎么样?这真的取决于你的值来自哪里(正如Pointy提到的)你没有逃过引号,试试:el.innerHTML=“
{{world}}”谢谢@t_G,我犯了一个关于双引号的愚蠢错误。之后将其更改为单引号。这很有效