如果没有 ID,有没有办法通过 JavaScript 访问 JSON-LD? [英] Is there a way to access JSON-LD via JavaScript if it doesn't have an ID?

查看:38
本文介绍了如果没有 ID,有没有办法通过 JavaScript 访问 JSON-LD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问我收到的 eventbrite 电子邮件中的内容,但 html 代码没有与 JSON-LD 脚本关联的 ID.那么有没有办法仍然访问这些数据?如果是,怎么办?

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?

是否可以将临时 ID 附加到 JSON-LD 脚本标签,以便我可以访问数据?如果是,怎么办?

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?

推荐答案

您可以获得所有 JSON-LD 块

You can get all JSON-LD blocks with

 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>

这篇关于如果没有 ID,有没有办法通过 JavaScript 访问 JSON-LD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆