javascript - 如何在此面包屑模式 JSON 生成器中显示位置编号
问题描述
请看一下这是我正在使用的代码:
<div id="app">
<h1>FAQPage Schema Form</h1>
<p>Add all your Questions and answers</p>
<div class="border" v-for="(field, index) in itemListElement">
<button class="removeBtn" @click="RemoveField(index)">
X
</button>
<textarea v-model="field.name" placeholder="PAGE NAME"></textarea><br/>
<textarea v-model="field.item" placeholder="URL"></textarea><br/>
</div>
<button class="addBtn" @click="AddField">
Add a Question
</button>
<h2>JSON Output</h2>
<p>After adding all your questions, add this code to your FAQ page</p>
<textarea id="codeArea">
<script type="application/ld+json">
{{$data | json }}
</script>
</textarea>
<div class="tooltip">
<button class="copyCode" onclick="copyFunction()" onmouseout="outFunc()">
<span class="tooltiptext" id="myTooltip">Copy to clipboard</span>
Copy Code</button>
</div>
</div>
这是脚本
function copyFunction() {
var copyText = document.getElementById("codeArea");
copyText.select();
document.execCommand("copy");
var tooltip = document.getElementById("myTooltip");
tooltip.innerHTML = "Copied code!";
}
function outFunc() {
var tooltip = document.getElementById("myTooltip");
tooltip.innerHTML = "Copy to clipboard";
}
//Begin Vue Code
new Vue({
el: "#app",
data: {
"@context": "https://schema.org",
"@type": "FAQPage",
mainEntity: []
},
created: function() {
this.mainEntity.push({
"@type": "Question",
name: "",
acceptedAnswer: { "@type": "Answer", text: "" }
});
},
methods: {
AddField: function() {
this.mainEntity.push({
"@type": "Question",
name: "",
acceptedAnswer: { "@type": "Answer", text: "" }
});
},
RemoveField(index) {
this.mainEntity.splice(index, 1);
}
}
});
您可以在这里看到它的实际效果: https ://codepen.io/adaprile/pen/orzxgr
当前显示的输出是这样的:(我已经添加了我想要显示数组项的索引号的位置。)
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"position": "WANT TO DISPLAY INDEX NUMBER HERE",
"name": "",
"acceptedAnswer": {
"@type": "Answer",
"text": ""
}
}
]
}
</script>
我可以在这里使用什么代码将数组项的索引显示为位置编号以 1 开头?
解决方案
据我了解,您想将数组元素的索引添加到元素本身。因此,每当您将元素推入数组时,您都可以检查数组长度:
this.mainEntity.push({
"@type": "Question",
name: "",
position: this.mainEntity.length+1,
acceptedAnswer: { "@type": "Answer", text: "" }
});
推荐阅读
- javascript - 具有相同类的多个按钮的单击事件的Javascript
- java - 方法没有在android中被调用
- regex - 如何在perl中匹配包含精确3次出现特殊字符的字符串
- jboss - JBoss EAP 7.0 检查启动
- angularjs - 如何在AngularJS中按值排序字典
- django - 涟漪 RPC 方法 account_tx 'date' 属性问题
- mysql - 快递和mysql。行未定义
- python - Django框架中的“登录”错误
- flask - flask babel 不适用于视图返回的字符串
- woocommerce - Woocommerce 在导航上移动欢迎消息