我正在尝试访问我收到的 eventbrite 电子邮件中的内容,但 html 代码没有与 JSON-LD 脚本关联的 ID。那么有没有办法仍然访问这些数据呢?如果是这样,怎么办?
是否可以将临时 ID 附加到 JSON-LD 脚本标记,以便我可以访问数据?如果是这样,怎么办?
最佳答案
您可以获得所有 JSON-LD block
document.querySelectorAll('script[type="application/ld+json"]');
或者只是第一个
document.querySelector('script[type="application/ld+json"]');
这是一个完整的示例:
var jsonld = JSON.parse(document.querySelector('script[type="application/ld+json"]').innerText);
document.getElementById('result').innerText = jsonld.endDate;
<html>
<head>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Event",
"name": "A random event",
"startDate": "2013-09-14T21:30",
"endDate": "2013-09-14T21:30"
}
</script>
</head>
<body>
<p>The end date is: <strong id="result"></strong></p>
</body>
</html>
关于javascript - 如果 JSON-LD 没有 ID,有没有办法通过 JavaScript 访问它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48812716/