我仍然是 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 商店的代碼。 感謝您的任何回答/意見/建議!