[英]How to display a script tag with json content in a code block in html?
I want to display a chunk of code generated dynamically inside a code example block that users can highlight a copy.我想在代码示例块内显示一块动态生成的代码,用户可以突出显示一个副本。
The content will change based on user input so it cannot be hardcoded.内容将根据用户输入而更改,因此无法硬编码。
This is an example of the content I would like to display inside the block:这是我想在块内显示的内容示例:
<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>
I'm using VueJS and this is the method in progress:我正在使用 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);
},
I've tried "code" and "pre" and all it shows is nothing but the script is there.我试过“code”和“pre”,它显示的只是脚本在那里。 I think the script is getting compiled and not shown as text, I could be wrong.... I hope that makes sense.我认为脚本正在编译并且没有显示为文本,我可能是错的......我希望这是有道理的。
output goes here:输出在这里:
<div class="form-group">
<pre >
<code id="resultCode">
</code>
</pre>
</div>
This can be done with string interpolation in a Vue template.这可以通过 Vue 模板中的字符串插值来完成。
Add a data property (eg, named " code
") to your component.向您的组件添加一个数据属性(例如,命名为“ code
”)。
data() { return { code: '' } }
Edit your template to interpolate that data property:编辑您的模板以插入该数据属性:
<div class="form-group"> <pre> <code id="resultCode"> {{code}} </code> </pre> </div>
Set the data property to the desired raw HTML (ie, the <script>
block you mentioned in question):将 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 } }
Note this method requires escaping the closing <script>
tag to avoid a syntax error .请注意,此方法需要转义结束<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
of the temp element into a text node.将 temp 元素的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.