【发布时间】:2022-01-20 19:52:40
【问题描述】:
<script>
(function(){
var data = {
"@context": "https://schema.org",
"@type": "Movie",
"actor": [],
for (i = 0; i < document.querySelectorAll('span.movie-cast-title').length; i++)
{
if (!document.querySelectorAll('span.gcharacter')[i]) {
data.actor.push({
"@type": "PerformanceRole",
"actor": {
"@type": "Person",
"name": document.querySelectorAll('span.movie-cast-title')[i].innerText,
"url": document.querySelectorAll('a.movie-cast-url')[i].href,
},
"characterName": document.querySelectorAll('span.gcharacter')[i].innerText,
});
}
else
{
data.actor.push({
"@type": "PerformanceRole",
"actor": {
"@type": "Person",
"name": document.querySelectorAll('span.movie-cast-title')[i].innerText,
"url": document.querySelectorAll('a.movie-cast-url')[i].href,
},
});
}
};
var script = document.createElement('script');
script.type = "application/ld+json";
script.innerHTML = JSON.stringify(data);
document.getElementsByTagName('head')[0].appendChild(script);
})(document);
</script>
如何跳过“characterName”:document.querySelectorAll('span.gcharacter')[i].innerText,值为空。 for is const characterName is does not exist 仅跳过 characterName var。 if else 语句是正确的还是使用其他 any 语句?
【问题讨论】:
标签: if-statement schema google-tag-manager