简体   繁体   English

如何更新HTML脚本标签中嵌入的数据?

[英]How do I update data embedded in the HTML script tag?

I have a simple html page with Json-ld data embedded in the script tag. 我有一个简单的html页面,其中在script标签中嵌入了Json-ld数据。

How do I update this data on the submit of a form? 如何在提交表单时更新此数据?

Here is some of the code of the page (data + load function). 这是页面的一些代码(数据+加载功能)。 What I'm missing is the saveData function that updates the data embedded in the script section. 我缺少的是saveData函数,该函数可更新脚本部分中嵌入的数据。

Thank you, 谢谢,

    <script id="person" type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Person",
  "givenName": "Marco",
  "familyName": "Van Basten"
}
</script>
<script>
function loadData() {

  var person = document.getElementById("person").textContent;
  var jsonld = JSON.parse(person);


  document.getElementById("givenName").value = jsonld.givenName;
  document.getElementById("familyName").value = jsonld.familyName;

}

The following code snippet is for example only , but should show you how to update your script object with the JSON string using JSON.stringify . 以下代码段仅作为示例 ,但应向您展示如何使用JSON.stringify使用JSON字符串更新脚本对象。

  person.innerHTML = JSON.stringify(jsonld);

Load the original by using Load. 通过使用加载来加载原件。 Change the values then Save and Load again. 更改值,然后再次保存并加载。 Use the browser developer tools to watch the values of the DOM. 使用浏览器开发人员工具观察DOM的值。

 function loadData(e) { var person = document.getElementById("person"); var jsonld = JSON.parse(person.textContent); console.log(jsonld.givenName, jsonld); document.getElementById("givenName").value = jsonld.givenName; document.getElementById("familyName").value = jsonld.familyName; } function saveData(e) { console.log('event', e); var person = document.getElementById("person"); var jsonld = JSON.parse(person.textContent); jsonld.givenName =document.getElementById("givenName").value; jsonld.familyName =document.getElementById("familyName").value; //person.innerHTML = JSON.stringify(jsonld); person.textContent = JSON.stringify(jsonld); } document.getElementById("saveButton").addEventListener('click', saveData); document.getElementById("loadButton").addEventListener('click',loadData); 
 <script id="person" type="application/ld+json"> { "@context": "http://schema.org", "@type": "Person", "givenName": "Marco", "familyName": "Van Basten" } </script> <form> <input type="text" id="givenName" /> <input type="text" id="familyName" /> </form> <button id="loadButton">Load</button> <button id="saveButton">Save</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 HTML 脚本标签复制到外部 javascript? - How do I copy HTML script tag to external javascript? 如何通过以下方式将 JSX 导入 HTML 页面<script> tag? - How do I import JSX to an HTML page via the <script> tag? 如何调试HTML脚本标记中编写的JavaScript - How do I debug JavaScript written within a script tag in the HTML 如何添加<script> tag as a simple string without it being treated as a regular <script> tag in HTML? - How do I add a <script> tag as a simple string without it being treated as a regular <script> tag in HTML? 如何从脚本标记中读取JS作为数据? - How do I read JS as data from a script tag? 如何将 json 数据转换为 Html 标签? - how do I convert json data into Html tag? 如何在脚本中获取HTML标签数据? - How to get html tag data in script? 如何在我的 html 中使用 json 脚本<script> tag and populate the data in the <img src=???/> - How can I use the json script on my html in the <script> tag and populate the data in the <img src=???/> 如何设置浏览器更新脚本标记以显示其使用的浏览器名称? - How to do I set the browser-update script tag to display the name of the browser it is using? 如何在页面加载之前更改 HTML 中脚本标记中的变量? - How do I change a variable in a script tag in HTML before the page has loaded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM