在使用JavaScript自定义(文档)的页面上显示Shopify购买按钮两次.gettelementbyid到docu



我需要在网页上显示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>

但是,这些更改导致buy按钮从两个表中消失。

我也试过把它改成getElementByClass

node: document.getElementByClass('buybutton'),

与HTML<div class="buybutton"></div>,但这也没有工作(没有出现)。

我做错了什么?我想做的事还有别的办法吗?

以下是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"
}
}
}
},
});
});
}
})();
/*]]>*/

试图搅乱现有代码太过深入和冒险。最简单和最安全的解决方案是有第二个<button>点击真正的<button>每当它被点击。用很少的代码实现相同的功能。注意,第二个<button>class="buybutton">——永远不要重复#id。

document.querySelector('.buybutton').onclick = clickBuyButton;
function clickBuyButton(e) {
document.getElementById('buybutton').click();
}

顺便说一句,本例中的<button>s也可以与<div>s一起工作。

单击第一个<button>,然后尝试单击第二个<button>

/*
This part is just functional for demonstration purposes. 
Whatever happens when #buybutton is clicked is of no consequence.
*/
document.getElementById('buybutton').onclick = buy;
function buy(e) {
const clicked = e.target;
if (clicked.matches('#buybutton')) {
document.getElementById('shop').classList.toggle('open');
}
}
/*
This is the second <button>. It's only purpose is to click
#buybutton whenever it is clicked
*/
document.querySelector('.buybutton').onclick = clickBuyButton;
function clickBuyButton(e) {
document.getElementById('buybutton').click();
}
fieldset {
min-height: 30vh;
}
#shop {
transform: scaleY(0);
transition: 0.5s;
}
#shop.open {
transform:scaleY(1);
transition: 0.3s;
}
<form>
<fieldset>
<button id='buybutton' type='button'>BUY</button>
</fieldset>
<fieldset id='shop'>
<legend>BUY STUFF NOW!</legend>
</fieldset>
<fieldset>
<button class='buybutton' type='button'>BUY</button>
</fieldset>
</form>

最新更新