【发布时间】:2021-12-01 07:32:53
【问题描述】:
我想根据网页中ID为#content的div的内容自动添加FAQ schema。
我不是开发人员,我正在构建 webflow 并在需要脚本时随时随地学习。
到目前为止,这是我得到的:
var questions = document.getElementsByClassName("auto-question");
var answer = document.getElementsByClassName("auto-answer");
var schema ={
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": []
}
for (var i = 0; i<questions.length; i++){
var new_schema =
{
"@type": "Question",
"name": questions[i].innerText,
"acceptedAnswer": {
"@type": "Answer",
"text": answer[i].innerText
}
}
schema.mainEntity.push(new_schema);
}
var script = document.createElement('script');
script.type = "application/ld+json";
script.text = JSON.stringify(schema);
document.querySelector('body').appendChild(script);
它有效,但现在我的问题是我想自动添加类 .auto-question 和 .auto-answer。
这是我正在尝试处理的页面示例:crédit impôt recherche。
我的想法是以下流程:
- 对于每个以问号结尾的 H2 标题
- 将类 .auto-question 添加到 H2
- 将类 .auto-answer 添加到 innerText 直到下一个 H2 或直到 #content 如果没有结束
再次重申,我不是开发人员,将一些代码放在一起没问题,但我自己的代码是不适合的。
希望你能帮忙, 非常感谢
【问题讨论】:
标签: javascript web schema.org json-ld