我仍然是 JavaScript 的初学者,正在尝试使用它来使 Etsy Mini API 响应。 我有两个<script>
需要添加到<div>
以便迷你商店出现:
<script type='text/javascript' src='https://www.etsy.com/assets/js/etsy_mini_shop.js'></script>
<script type='text/javascript'>new Etsy.Mini(<UNIQUE_SHOP_ID>,'thumbnail',4,3,0,'https://www.etsy.com');</script>
在第二个<script>
中,我可以指定缩略图网格的大小(上例中为 4x3),但该网格没有响应性,因此如果它加载到屏幕较小的设备上,或者笔记本电脑的浏览器窗口调整大小后,4x3 网格可能太大。 我想在外部 .js 文件中使用媒体查询来更改此缩略图网格,这就是我遇到问题的地方。
以下是我当前的外部 .js 文件:
var products = document.getElementById("products-card");
var grid5x3 = "<script type='text/javascript'>new Etsy.Mini(<UNIQUE_SHOP_ID>,'thumbnail',5,3,0,'https://www.etsy.com');</script>";
var grid4x3 = "<script type='text/javascript'>new Etsy.Mini(<UNIQUE_SHOP_ID>,'thumbnail',4,3,0,'https://www.etsy.com');</script>";
var grid3x3 = "<script type='text/javascript'>new Etsy.Mini(<UNIQUE_SHOP_ID>,'thumbnail',3,3,0,'https://www.etsy.com');</script>";
var grid2x3 = "<script type='text/javascript'>new Etsy.Mini(<UNIQUE_SHOP_ID>,'thumbnail',2,3,0,'https://www.etsy.com');</script>";
var grid1x3 = "<script type='text/javascript'>new Etsy.Mini(<UNIQUE_SHOP_ID>,'thumbnail',1,3,0,'https://www.etsy.com');</script>";
(function () {
function resize() {
var min_2500 = window.matchMedia('only screen and (min-width: 2500px)').addListener(function () {
products.innerText = grid5x3;
});
var min_1900_max_2499 = window.matchMedia('only screen and (min-width: 1900px) and ' +
'(max-width: 2499px)').addListener(function () {
products.innerText = grid4x3;
});
var min_1200_max_1899 = window.matchMedia('only screen and (min-width: 1200px) and ' +
'(max-width: 1899px)').addListener(function () {
products.innerText = grid3x3;
});
var min_900_max_1199 = window.matchMedia('only screen and (min-width: 900px) and ' +
'(max-width: 1199px)').addListener(function () {
products.innerText = grid2x3;
});
var max_899 = window.matchMedia('only screen and (max-width: 899px)').addListener(function () {
products.innerText = grid1x3;
});
}
resize();
}());
这是.html:
<head>
...
<script type='text/javascript' src='https://www.etsy.com/assets/js/etsy_mini_shop.js'></script>
</head>
<body>
...
<div class="products-card" id="products-card"></div>
...
</body>
问题 1:脚本没有被执行,而是实际的文本,包括<script>
标签,出现在<div>
中。 如何更改我的 Javascript 以使脚本执行并呈现迷你商店?
问题 2:页面加载时<div>
为空,但在调整浏览器屏幕大小并达到媒体查询中指定的截止点之一后,文本会根据媒体查询正确显示并更改。 如何使商店在页面加载时呈现在<div>
中,而不仅仅是在调整浏览器窗口大小时?
更新 1所以我发现使用 innerText/innerHTML 在注入 html 文件时不允许执行/渲染任何脚本( 基于此处的答案)。 这是我的新 .js 文件:
var products = document.getElementById("products-card");
var grid5x3 = "new Etsy.Mini(<UNIQUE_SHOP_ID>,'thumbnail',5,3,0,'https://www.etsy.com');";
var grid4x3 = "new Etsy.Mini(<UNIQUE_SHOP_ID>,'thumbnail',4,3,0,'https://www.etsy.com');";
var grid3x3 = "new Etsy.Mini(<UNIQUE_SHOP_ID>,'thumbnail',3,3,0,'https://www.etsy.com');";
var grid2x3 = "new Etsy.Mini(<UNIQUE_SHOP_ID>,'thumbnail',2,3,0,'https://www.etsy.com');";
var grid1x3 = "new Etsy.Mini(<UNIQUE_SHOP_ID>,'thumbnail',1,3,0,'https://www.etsy.com');";
(function () {
function resize() {
var min_2500 = window.matchMedia('only screen and (min-width: 2500px)').addListener(function () {
var eval_grid5x3 = eval(grid5x3);
products.innerText = eval_grid5x3;
});
var min_1900_max_2499 = window.matchMedia('only screen and (min-width: 1900px) and ' +
'(max-width: 2499px)').addListener(function () {
var eval_grid4x3 = eval(grid4x3);
products.innerText = eval_grid4x3;
});
var min_1200_max_1899 = window.matchMedia('only screen and (min-width: 1200px) and ' +
'(max-width: 1899px)').addListener(function () {
var eval_grid3x3 = eval(grid3x3);
products.innerText = eval_grid3x3;
});
var min_900_max_1199 = window.matchMedia('only screen and (min-width: 900px) and ' +
'(max-width: 1199px)').addListener(function () {
var eval_grid2x3 = eval(grid2x3);
products.innerText = eval_grid2x3;
});
var max_899 = window.matchMedia('only screen and (max-width: 899px)').addListener(function () {
var eval_grid1x3 = eval(grid1x3);
products.innerText = eval_grid1x3;
});
}
resize();
}());
虽然这确实渲染了 Etsy 迷你商店,但它有一些奇怪的行为:
当我期望目标<div>
被脚本替换时,整个<body>
被注入的脚本替换。
每次我调整屏幕大小时,它都会添加另一个 Etsy 迷你商店(它不会替换当前存在的那个),导致页面充满了许多商店。
它没有在页面加载时呈现的问题仍然存在。
抱歉,如果这是一个相当简单的问题。 我花了很多时间谷歌搜索解决方案,但我尝试过的都没有奏效。 我上面的代码是我最接近响应式 Etsy Mini 商店的代码。 感谢您的任何回答/意见/建议!