zz-shopify-components
Version:
Reusable Shopify components for theme projects
53 lines (47 loc) • 1.71 kB
JavaScript
// 用来处理多国家多跳转的问题
// el: 传入的元素
// links: 站点地图文本
var bindSiteJump = bindSiteJump
? bindSiteJump
: function bindSiteJump(el, links) {
if (!el || !links) {
console.log('SiteJump: el and links are required');
return;
// throw new Error('SiteJump: el and links are required');
}
const SITE_JUMP_MAP = links;
const arr = SITE_JUMP_MAP.split('\n'); // 按换行符拆分为数组
const siteMap = {};
// 将站点地图解析成对象
arr.forEach((item) => {
const [key, value] = item.split(': ');
if (key && value) {
siteMap[key.trim()] = value.trim();
}
});
document.addEventListener('DOMContentLoaded', function (event) {
const countryElement =
document.getElementsByClassName('countryWord')[0];
const country = countryElement.innerText.trim();
const targetUrl = siteMap[country] || siteMap['default'] || '/';
if (el.tagName.toLowerCase() === 'a') {
if (countryElement) {
el.href = targetUrl;
}
}
});
// 绑定点击事件
el.onclick = () => {
console.log(123412342);
const countryElement =
document.getElementsByClassName('countryWord')[0];
if (countryElement) {
const country = countryElement.innerText.trim();
const targetUrl = siteMap[country] || siteMap['default'] || '/';
// window.location.href = targetUrl; // 跳转到匹配的站点
toSubSiteBP(targetUrl);
} else {
console.error('SiteJump: Country element not found');
}
};
};