【发布时间】:2019-04-11 09:55:18
【问题描述】:
我想在一个代码示例块中显示一段动态生成的代码,用户可以突出显示一个副本。
内容会根据用户输入而改变,因此无法硬编码。
这是我想在块内显示的内容示例:
<script type="application/ld+json">{
"name": "PBOV1 Is a Human De Novo Gene with Tumor-Specific Expression That Is Associated with a Positive Clinical Outcome of Cancer",
"keywords": "pbov1, tumor-specific, cancer, Cancer, Evolutionary Biology, Immunology",
"version": "1",
"url": "https://figshare.com/articles/PBOV1_Is_a_Human_De_Novo_Gene_with_Tumor_Specific_Expression_That_Is_Associated_with_a_Positive_Clinical_Outcome_of_Cancer__/156778",
"license": ""
}</script>
我正在使用 VueJS,这是正在进行的方法:
makeScript(){
var str = JSON.stringify(this.metadata, null, 2);
var script=document.createElement('script');
script.type='application/ld+json';
script.text = str;
this.result = script;
document.getElementById("resultCode").appendChild(script);
},
我试过“code”和“pre”,但它显示的只是脚本而已。我认为脚本正在编译并且没有显示为文本,我可能是错的......我希望这是有道理的。
输出到这里:
<div class="form-group">
<pre >
<code id="resultCode">
</code>
</pre>
</div>
【问题讨论】:
-
是的,成功了!谢谢@ChrisG
标签: javascript html vue.js