这是我的代码:
$( document ).ready(function() {
$.getJSON('onlineinsurance/services/aggregateRating.php', { rating: false } , function(result) {
$("#rating-value").text(result['avg_rating']);
$("#rating-count").text(result['count']);
schema_org = ` <script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Product",
"name": "Some name",
"image": "some image path",
"description": "some description",
"brand": "some brand name",
"offers": {
"@type": "AggregateOffer",
"priceCurrency": "BGN",
"lowPrice": "176.93",
"offerCount": 10
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "'` + result['avg_rating'] + `'",
"reviewCount": "'` + result['count'] + `'"
}
}
</script>
`;
$("#schema-org").after(schema_org);
});
});
和 HTML:
<p id="schema-org"></p>
我无法处理问题的根源,如果我更改此行:
$("#schema-org").after(schema_org);
至
$("#schema-org").after('test');
例如,它工作得很好,所以我认为多行字符串是问题所在?我错了吗?
编辑:好吧..主要问题出在我身上,当我用 cntrl + u 检查时,字符串没有出现,但当我用 f12 检查时,它就在那里!
最佳答案
您可以尝试用此替换您的 schema_org 值吗?
` <script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Product",
"name": "Some name",
"image": "some image path",
"description": "some description",
"brand": "some brand name",
"offers": {
"@type": "AggregateOffer",
"priceCurrency": "BGN",
"lowPrice": "176.93",
"offerCount": 10
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "'${result['avg_rating']}'",
"reviewCount": "'${result['count']}'"
}
}
<\/script>
`;
并尝试。我认为问题在于您的脚本被正确选取
关于javascript - 无法将多行字符串输出为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58097829/