我正在使用javascript修改html脚本标签中的“ application / ld + json”,但我无法选择json值并使用原始js进行更改。
hexo 3.9操作系统:Linux 4.4.0-18362-Microsoft Linux x64节点: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>
建立了testvar,稍后再尝试
document.getElementById("myjsonid")["firstChild"]
Web控制台输出
#text "
{
"@context": "https://schema.org",
"@type": "BlogPosting",
"mainEntityOfPage": "https://blog.tinyume.com/archives/material-geng-duo-xin-xi.html",
"headline": "让 Material 文章更加人性化",
"datePublished": "Tue Oct 01 2019 23:10:21 GMT+0800",
"dateModified": "Sat Oct 26 2019 14:35:20 GMT+0800",
"image": "",
"author": {
"@type": "Person",
"name": "黯梦萦辰",
"image": {
"@type": "ImageObject",
"url": "https://cdn.jsdelivr.net/gh/iyume/static/hexo-blog/img/avatar.png"
},
"description": "仰望半月的夜空"
},
"publisher": {
"@type": "Organization",
"name": "黯梦萦辰",
"logo": {
"@type": "ImageObject",
"url": "https://cdn.jsdelivr.net/gh/iyume/static/hexo-blog/img/avatar.png"
}
},
"keywords": "黯梦萦辰,iyume,tinyume,博客",
"description": "给 Material 文章页添加字数统计、阅读时长和分类标签"
}
"
但是现在我不知道该怎么办,我会错过一些api文档吗?
firstChild
给您一个带有脚本文本的Text节点。您可以同时使用其nodeValue
属性(spec,MDN)来获取和设置文本:
const script = document.getElementById("myjsonid");
script.firstChild.nodeValue = '{"foo": 1}'; // Completely replace it
console.log(script.firstChild.nodeValue);
实时示例:
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>
您也可以在textContent
元素本身上使用spec(MDN,innerText
)或spec(MDN,script
):
const script = document.getElementById("myjsonid");
script.firstChild.nodeValue = '{}'; // Completely replace it
console.log(script.firstChild.nodeValue);
实时示例:
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>
在评论中,您曾说过要更改结构的一个特定部分。为此,请在字符串上使用JSON.parse
获取JSON的对象树,在该树中进行更改,然后使用JSON.stringify
获取JSON字符串以写回script
元素:
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);
实时示例:
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>