[英]How to display a script tag with json content in a code block in html?
我想在代码示例块内显示一块动态生成的代码,用户可以突出显示一个副本。
内容将根据用户输入而更改,因此无法硬编码。
这是我想在块内显示的内容示例:
<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>
这可以通过 Vue 模板中的字符串插值来完成。
向您的组件添加一个数据属性(例如,命名为“ code
”)。
data() { return { code: '' } }
编辑您的模板以插入该数据属性:
<div class="form-group"> <pre> <code id="resultCode"> {{code}} </code> </pre> </div>
将 data 属性设置为所需的原始 HTML(即,您提到的<script>
块):
methods: { setCode() { this.code = ` <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> `; // Must escape closing script tag } }
请注意,此方法需要转义结束<script>
标记以避免语法错误。
new Vue({ el: '#app', data: () => ({ code: '', }), mounted() { this.setCode(); }, methods: { setCode() { this.code = `<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>`; } } })
<script src="https://unpkg.com/vue@2.5.17"></script> <div id="app"> <div class="form-group"> <pre><code>{{code}}</code></pre> </div> </div>
innerHTML
放入文本节点。 function makeScript() { metadata = { "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": "" } var str = JSON.stringify(metadata, null, 2); var script = document.createElement('script'); script.type = 'application/ld+json'; script.text = str; p = document.createElement('div'); p.appendChild(script); text = document.createTextNode(p.innerHTML); document.getElementById("resultCode").appendChild(text); } makeScript();
<div class="form-group"> <pre><code id="resultCode"></code></pre> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.