使用JavaScript自定义显示Shopify购买按钮两次(document.getElementById to document.queryselectorall)
我需要在网页上两次显示Shopify购买按钮(链接到同一产品),因为我有一个智能手机表和PC表。
借助我从Shopify获得的生成的代码,该按钮仅出现在一个表上,因为此JavaScript:
ShopifyBuy.UI.onReady(client).then(function (ui) {
ui.createComponent('product', {
id: '7369316827224',
node: document.getElementById('buybutton'),
“ document.getElementById”仅允许显示一次ID。 因此,为了能够返回具有ID =“ buybutton”的所有元素,我将其更改为以下内容:
ShopifyBuy.UI.onReady(client).then(function (ui) {
ui.createComponent('product', {
id: '7369316827224',
node: document.querySelectorAll('#buybutton'),
此外,我将HTML更改为
<div id='buybutton'></div>
“
<div id="#buybutton"></div>
但这些更改”,这些更改会导致购买按钮从两张表中消失。
我还尝试将其更改为
node: document.getElementByClass('buybutton'),
使用HTML &lt; div class =“ buybutton”&gt;&lt;/div&gt;
getElementByClass,但这也不起作用(什么都没出现)。
我在做什么错?还有另一种方法可以做我想做的事吗?
这是Shopify供参考的整个脚本(由于隐私原因,某些信息被“ 000”替换):
/*<![CDATA[*/
(function () {
var scriptURL = 'https://sdks.shopifycdn.com/buy-button/latest/buy-button-storefront.min.js';
if (window.ShopifyBuy) {
if (window.ShopifyBuy.UI) {
ShopifyBuyInit();
} else {
loadScript();
}
} else {
loadScript();
}
function loadScript() {
var script = document.createElement('script');
script.async = true;
script.src = scriptURL;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);
script.onload = ShopifyBuyInit;
}
function ShopifyBuyInit() {
var client = ShopifyBuy.buildClient({
domain: 'sugatsune-uk.myshopify.com',
storefrontAccessToken: '000',
});
ShopifyBuy.UI.onReady(client).then(function (ui) {
ui.createComponent('product', {
id: '7369316827224',
node: document.getElementById('buybutton'),
moneyFormat: '%C2%A3%7B%7Bamount%7D%7D',
options: {
"product": {
"styles": {
"product": {
"@media (min-width: 601px)": {
"max-width": "calc(25% - 20px)",
"margin-left": "20px",
"margin-bottom": "50px"
}
},
"button": {
"font-weight": "bold",
"font-size": "12px",
"padding-top": "6px",
"padding-bottom": "6px",
":hover": {
"background-color": "#2d2d2d"
},
"background-color": "#000000",
":focus": {
"background-color": "#2d2d2d"
},
"padding-left": "6px",
"padding-right": "6px"
},
"quantityInput": {
"font-size": "12px",
"max-width": "20px",
"padding-top": "6px",
"padding-bottom": "6px"
}
},
"contents": {
"img": false,
"button": false,
"buttonWithQuantity": true,
"title": false,
"price": false
},
"text": {
"button": "Add to cart"
}
},
"productSet": {
"styles": {
"products": {
"@media (min-width: 601px)": {
"margin-left": "-20px"
}
}
}
},
"modalProduct": {
"contents": {
"img": false,
"imgWithCarousel": true,
"button": false,
"buttonWithQuantity": true
},
"styles": {
"product": {
"@media (min-width: 601px)": {
"max-width": "100%",
"margin-left": "0px",
"margin-bottom": "0px"
}
},
"button": {
"font-weight": "bold",
"font-size": "12px",
"padding-top": "6px",
"padding-bottom": "6px",
":hover": {
"background-color": "#2d2d2d"
},
"background-color": "#000000",
":focus": {
"background-color": "#2d2d2d"
},
"padding-left": "6px",
"padding-right": "6px"
},
"quantityInput": {
"font-size": "12px",
"max-width": "20px",
"padding-top": "5px",
"padding-bottom": "5px"
}
},
"text": {
"button": "Add to cart"
}
},
"option": {},
"cart": {
"styles": {
"button": {
"font-weight": "bold",
"font-size": "14px",
"padding-top": "15px",
"padding-bottom": "15px",
":hover": {
"background-color": "#2d2d2d"
},
"background-color": "#000000",
":focus": {
"background-color": "#2d2d2d"
}
}
},
"text": {
"total": "Subtotal",
"button": "Checkout"
}
},
"toggle": {
"styles": {
"toggle": {
"font-weight": "bold",
"background-color": "#000000",
":hover": {
"background-color": "#2d2d2d"
},
":focus": {
"background-color": "#2d2d2d"
}
},
"count": {
"font-size": "14px"
}
}
}
},
});
});
}
})();
/*]]>*/
I need to display a Shopify buy button twice (links to the same product) on a web page because I have a smartphone table and pc table.
With the generated code I got from Shopify, the button only appears on one table because of this JavaScript:
ShopifyBuy.UI.onReady(client).then(function (ui) {
ui.createComponent('product', {
id: '7369316827224',
node: document.getElementById('buybutton'),
The "document.getElementById" only allows for an ID to display once.
So in order to be able return all elements with id="buybutton", I changed it to the following:
ShopifyBuy.UI.onReady(client).then(function (ui) {
ui.createComponent('product', {
id: '7369316827224',
node: document.querySelectorAll('#buybutton'),
Additionally, I changed the HTML from
<div id='buybutton'></div>
to
<div id="#buybutton"></div>
However, these changes cause the buy button to disappear from both tables.
I have also tried changing it to getElementByClass
node: document.getElementByClass('buybutton'),
with HTML <div class="buybutton"></div>
but this also did not work (nothing appeared).
What am I doing wrong? Is there another way to do what I want to do?
Here is the entire script from Shopify for reference (some info is replaced with "000" for privacy reasons):
/*<![CDATA[*/
(function () {
var scriptURL = 'https://sdks.shopifycdn.com/buy-button/latest/buy-button-storefront.min.js';
if (window.ShopifyBuy) {
if (window.ShopifyBuy.UI) {
ShopifyBuyInit();
} else {
loadScript();
}
} else {
loadScript();
}
function loadScript() {
var script = document.createElement('script');
script.async = true;
script.src = scriptURL;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);
script.onload = ShopifyBuyInit;
}
function ShopifyBuyInit() {
var client = ShopifyBuy.buildClient({
domain: 'sugatsune-uk.myshopify.com',
storefrontAccessToken: '000',
});
ShopifyBuy.UI.onReady(client).then(function (ui) {
ui.createComponent('product', {
id: '7369316827224',
node: document.getElementById('buybutton'),
moneyFormat: '%C2%A3%7B%7Bamount%7D%7D',
options: {
"product": {
"styles": {
"product": {
"@media (min-width: 601px)": {
"max-width": "calc(25% - 20px)",
"margin-left": "20px",
"margin-bottom": "50px"
}
},
"button": {
"font-weight": "bold",
"font-size": "12px",
"padding-top": "6px",
"padding-bottom": "6px",
":hover": {
"background-color": "#2d2d2d"
},
"background-color": "#000000",
":focus": {
"background-color": "#2d2d2d"
},
"padding-left": "6px",
"padding-right": "6px"
},
"quantityInput": {
"font-size": "12px",
"max-width": "20px",
"padding-top": "6px",
"padding-bottom": "6px"
}
},
"contents": {
"img": false,
"button": false,
"buttonWithQuantity": true,
"title": false,
"price": false
},
"text": {
"button": "Add to cart"
}
},
"productSet": {
"styles": {
"products": {
"@media (min-width: 601px)": {
"margin-left": "-20px"
}
}
}
},
"modalProduct": {
"contents": {
"img": false,
"imgWithCarousel": true,
"button": false,
"buttonWithQuantity": true
},
"styles": {
"product": {
"@media (min-width: 601px)": {
"max-width": "100%",
"margin-left": "0px",
"margin-bottom": "0px"
}
},
"button": {
"font-weight": "bold",
"font-size": "12px",
"padding-top": "6px",
"padding-bottom": "6px",
":hover": {
"background-color": "#2d2d2d"
},
"background-color": "#000000",
":focus": {
"background-color": "#2d2d2d"
},
"padding-left": "6px",
"padding-right": "6px"
},
"quantityInput": {
"font-size": "12px",
"max-width": "20px",
"padding-top": "5px",
"padding-bottom": "5px"
}
},
"text": {
"button": "Add to cart"
}
},
"option": {},
"cart": {
"styles": {
"button": {
"font-weight": "bold",
"font-size": "14px",
"padding-top": "15px",
"padding-bottom": "15px",
":hover": {
"background-color": "#2d2d2d"
},
"background-color": "#000000",
":focus": {
"background-color": "#2d2d2d"
}
}
},
"text": {
"total": "Subtotal",
"button": "Checkout"
}
},
"toggle": {
"styles": {
"toggle": {
"font-weight": "bold",
"background-color": "#000000",
":hover": {
"background-color": "#2d2d2d"
},
":focus": {
"background-color": "#2d2d2d"
}
},
"count": {
"font-size": "14px"
}
}
}
},
});
});
}
})();
/*]]>*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试弄乱现有代码是太多的挖掘和风险。最简单,最安全的解决方案是让第二个
&lt; butt&gt;
单击实际&lt;
,每当单击时。这是相同的功能,几乎没有代码。请注意,第二个&lt; button&gt;
具有 class =“ buybutton” - 永不复制#ids。顺便说一句,
&lt; button&gt;
在此示例中也将与&lt; div; gt;
s一起使用。单击第一个
&lt; button&gt;
,然后尝试单击第二个&lt; button&gt;
It's too much digging and risky to try to mess with the existing code. The easiest and safest solution is to have the second
<button>
click the real<button>
whenever it is clicked. It's the same functionality with very little code. Note, the second<button>
has class="buybutton" -- never duplicate #ids.BTW, the
<button>
s in this example will work with<div>
s as well.Click the first
<button>
, then try clicking the second<button>