在json ld应用程序上包含javascript变量
请告诉我这个脚本有什么问题。我在这个页面上找到了它,但它在我的网站上不起作用在json ld应用程序上包含javascript变量,javascript,json,Javascript,Json,请告诉我这个脚本有什么问题。我在这个页面上找到了它,但它在我的网站上不起作用 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script id="myJSONID" type="application/ld+json"></script> <script type="text/javascript">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script id="myJSONID" type="application/ld+json"></script>
<script type="text/javascript">
window.onload=function(){
var myList = [];
function buildMyList() {
return ["2 items", "3items", "so on"];
}
$("#myJSONID").text(function() {
return JSON.stringify({
"@context": "http://schema.org/",
"@type": "Recipe",
"recipeIngredient": buildMyList()
});
});
}
</script>
结果应该是:
<script id="myJSONID" type="application/ld+json">
{"@context":"http://schema.org/",
"@type":"Recipe",
"recipeIngredient":["2 items","3items","so on"]
}</script>
{@context”:http://schema.org/",
“@type”:“菜谱”,
“收款人”:[“2项”、“3项”、“等等”]
}
但加载页面时,myJSONID仍然为空:
<script id="myJSONID" type="application/ld+json"></script>
这是控制台中结果的屏幕截图:
使用JavaScript修改DOM不会更改页面的源代码。它只会更改内存中的DOM 使用DOM检查器工具(如Chrome开发工具中的“元素”面板)检查页面表明,您的代码确实成功地修改了DOM
查看源代码不会显示任何更改,这是预期的行为。它在您的网站上起作用。是什么让你认为它没有呢?我编辑这篇文章是为了告诉你我需要什么。是的,当我之前查看时,它出现在你的测试网站上。我不知道你为什么认为它不在那里。请给我一个你在控制台上看到的屏幕截图,因为那里什么都没有。RecipteInCredit值未替换为[“2项”、“3项”、“等等”]MyJSONID为空。我可以看到它不是空的。在这里,我粘贴了测试网站上脚本标记的内容:
{“@context”:http://schema.org/“,“@type”:“Recipe”,“recipeCredit”:[“2项”,“3项”,“等等”]}
<script id="myJSONID" type="application/ld+json"></script>