简体   繁体   English

如何在 html label 中使用 javascript 修改 type(ld+json) 值?

[英]How can I modify the type(ld+json) value with javascript in html label?

I'm using javascript to modify the "application/ld+json" in html script label, but I cannot select the json value and change it with raw js. I'm using javascript to modify the "application/ld+json" in html script label, but I cannot select the json value and change it with raw js.

hexo 3.9六边形 3.9
os: Linux 4.4.0-18362-Microsoft linux x64操作系统:Linux 4.4.0-18362-Microsoft linux x64
node: 13.0.1节点:13.0.1

<script id="myjsonid" type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "BlogPosting",
        "mainEntityOfPage": "<%- config.url + url_for(path) %>",
        "headline": "<%- page.title %>",
        "datePublished": "<%= page.date %>",
        "dateModified": "<%= page.updated %>",
        "image": "<%= page.thumbnail %>",
        "author": {
            "@type": "Person",
            "name": "<%= config.author %>",
            "image": {
                "@type": "ImageObject",
                "url": "<%= theme.img.avatar %>"
            },
            "description": "<%- theme.uiux.slogan %>"
        },
        "publisher": {
            "@type": "Organization",
            "name": "<%= config.title %>",
            "logo": {
                "@type": "ImageObject",
                "url": "<%= theme.head.high_res_favicon %>"
            }
        },
        "keywords": "<% if(page.tags && page.tags.each) { page.tags.each(function(tag) { %><%- tag.name + ',' %><% })} %><%= theme.head.keywords %>",
        "description": "<% if(page.description) { %><%= page.description %><% } else if(page.excerpt){ %><%= strip_html(page.excerpt).replace(/^s*/, '').replace(/s*$/, '') %><% } else if (config.description){ %><%= config.description %><% } %>"
    }
</script>
<script>
    roundsum = Math.round(Math.random()*"<%= theme.thumbnail.random_amount %>"+1);
    testvar = document.getElementById("myjsonid");
</script>

now I don't know what to do, do i miss some api documents?现在我不知道该怎么办,我错过了一些 api 文件吗?

firstChild is giving you a Text node with the text of the script. firstChild为您提供一个带有脚本文本的 Text 节点。 You can use its nodeValue property ( spec ,MDN ) both to get and set the text:您可以使用它的nodeValue属性( specMDN )来获取和设置文本:

const script = document.getElementById("myjsonid");
script.firstChild.nodeValue = '{"foo": 1}'; // Completely replace it
console.log(script.firstChild.nodeValue);

Live Example:现场示例:

 const script = document.getElementById("myjsonid"); script.firstChild.nodeValue = '{"foo": 1}'; // Completely replace it console.log(script.firstChild.nodeValue);
 <script id="myjsonid" type="application/ld+json"> { "@context": "https://schema.org", "@type": "BlogPosting", "mainEntityOfPage": "<%- config.url + url_for(path) %>", "headline": "<%- page.title %>", "datePublished": "<%= page.date %>", "dateModified": "<%= page.updated %>", "image": "<%= page.thumbnail %>", "author": { "@type": "Person", "name": "<%= config.author %>", "image": { "@type": "ImageObject", "url": "<%= theme.img.avatar %>" }, "description": "<%- theme.uiux.slogan %>" }, "publisher": { "@type": "Organization", "name": "<%= config.title %>", "logo": { "@type": "ImageObject", "url": "<%= theme.head.high_res_favicon %>" } }, "keywords": "<% if(page.tags && page.tags.each) { page.tags.each(function(tag) { %><%- tag.name + ',' %><% })} %><%= theme.head.keywords %>", "description": "<% if(page.description) { %><%= page.description %><% } else if(page.excerpt){ %><%= strip_html(page.excerpt).replace(/^s*/, '').replace(/s*$/, '') %><% } else if (config.description){ %><%= config.description %><% } %>" } </script>

You could also use textContent ( spec , MDN ) or innerText ( spec , MDN ) on the script element itself:您还可以在script元素本身上使用textContent ( spec , MDN ) 或innerText ( spec , MDN ):

const script = document.getElementById("myjsonid");
script.firstChild.nodeValue = '{}'; // Completely replace it
console.log(script.firstChild.nodeValue);

Live Example:现场示例:

 const script = document.getElementById("myjsonid"); script.textContent = '{"foo": 1}'; // Completely replace it console.log(script.firstChild.nodeValue); script.innerText = '{"foo": 2}'; // Completely replace it console.log(script.firstChild.nodeValue);
 <script id="myjsonid" type="application/ld+json"> { "@context": "https://schema.org", "@type": "BlogPosting", "mainEntityOfPage": "<%- config.url + url_for(path) %>", "headline": "<%- page.title %>", "datePublished": "<%= page.date %>", "dateModified": "<%= page.updated %>", "image": "<%= page.thumbnail %>", "author": { "@type": "Person", "name": "<%= config.author %>", "image": { "@type": "ImageObject", "url": "<%= theme.img.avatar %>" }, "description": "<%- theme.uiux.slogan %>" }, "publisher": { "@type": "Organization", "name": "<%= config.title %>", "logo": { "@type": "ImageObject", "url": "<%= theme.head.high_res_favicon %>" } }, "keywords": "<% if(page.tags && page.tags.each) { page.tags.each(function(tag) { %><%- tag.name + ',' %><% })} %><%= theme.head.keywords %>", "description": "<% if(page.description) { %><%= page.description %><% } else if(page.excerpt){ %><%= strip_html(page.excerpt).replace(/^s*/, '').replace(/s*$/, '') %><% } else if (config.description){ %><%= config.description %><% } %>" } </script>


In a comment you've said you want to change one specific part of the structure.在评论中你说过你想改变结构的一个特定部分。 To do that, use JSON.parse on the string to get an object tree for the JSON, make your change in that tree, and then use JSON.stringify to get a JSON string to write back to the script element: To do that, use JSON.parse on the string to get an object tree for the JSON, make your change in that tree, and then use JSON.stringify to get a JSON string to write back to the script element:

const script = document.getElementById("myjsonid");
const obj = JSON.parse(script.firstChild.nodeValue);
obj.image = "***THIS IS THE UPDATE***";
script.firstChild.nodeValue = JSON.stringify(obj);
console.log(script.firstChild.nodeValue);

Live Example:现场示例:

 const script = document.getElementById("myjsonid"); const obj = JSON.parse(script.firstChild.nodeValue); obj.image = "***THIS IS THE UPDATE***"; script.firstChild.nodeValue = JSON.stringify(obj); console.log(script.firstChild.nodeValue);
 <script id="myjsonid" type="application/ld+json"> { "@context": "https://schema.org", "@type": "BlogPosting", "mainEntityOfPage": "<%- config.url + url_for(path) %>", "headline": "<%- page.title %>", "datePublished": "<%= page.date %>", "dateModified": "<%= page.updated %>", "image": "<%= page.thumbnail %>", "author": { "@type": "Person", "name": "<%= config.author %>", "image": { "@type": "ImageObject", "url": "<%= theme.img.avatar %>" }, "description": "<%- theme.uiux.slogan %>" }, "publisher": { "@type": "Organization", "name": "<%= config.title %>", "logo": { "@type": "ImageObject", "url": "<%= theme.head.high_res_favicon %>" } }, "keywords": "<% if(page.tags && page.tags.each) { page.tags.each(function(tag) { %><%- tag.name + ',' %><% })} %><%= theme.head.keywords %>", "description": "<% if(page.description) { %><%= page.description %><% } else if(page.excerpt){ %><%= strip_html(page.excerpt).replace(/^s*/, '').replace(/s*$/, '') %><% } else if (config.description){ %><%= config.description %><% } %>" } </script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM