问题描述
我尝试访问我收到的eventbrite电子邮件中的内容,但该html代码没有与JSON-LD脚本关联的ID。那么有没有办法仍然可以访问这些数据?如果是这样,怎么做?
是否可以将临时ID附加到JSON-LD脚本标记中,以便我可以访问数据?如果是这样,怎么样?
您可以使用 $ b $获取所有JSON-LD块b
document.querySelectorAll('script [type =application / ld + json]');
或第一个包含
document.querySelector( '脚本[类型= 应用/ LD + JSON]');
下面是一个完整的例子:
< HTML> < HEAD> < script type =application / ld + json> {@context:http://schema.org,@type:事件,名称:随机事件,startDate:2013-09-14T21:30, endDate:2013-09-14T21:30}< / script> < /头> <身体GT; < p>结束日期为:< strong id =result>< / strong>< / p> < / body>< / html>
I am trying to access the content in eventbrite emails that I receive but the html code doesn't have an ID associated to the JSON-LD script. So is there a way to still access this data? If so, how?
Is it possible to perhaps attach a temporary id to the JSON-LD script tag so that I can access the data? If so, how?
You can get all JSON-LD blocks with
document.querySelectorAll('script[type="application/ld+json"]');
or just the first one with
document.querySelector('script[type="application/ld+json"]');
Here's a full example:
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>
这篇关于如果没有ID,是否有通过JavaScript访问JSON-LD的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!