【发布时间】:2021-04-18 09:34:07
【问题描述】:
我在下面提供了大约 30 个具有不同 ID 的选择器示例代码
document.getElementById("selector-1").onchange = function() {
document.getElementById("btn-1").href = "/?add-to-cart=" + this.selectedOptions[0].getAttribute('data-product') + "&" + "variation_id=" + this.value + "/";
}
document.getElementById("selector-2").onchange = function() {
document.getElementById("btn-2").href = "/?add-to-cart=" + this.selectedOptions[0].getAttribute('data-product') + "&" + "variation_id=" + this.value + "/";
}
document.getElementById("selector-3").onchange = function() {
document.getElementById("btn-3").href = "/?add-to-cart=" + this.selectedOptions[0].getAttribute('data-product') + "&" + "variation_id=" + this.value + "/";
}
能否请您告诉我如何将所有这些组合成更通用的东西,以免每次都使用不同的 ID 带来所有代码,因为我看到目前这是非常未优化的代码。不幸的是,我的知识不足以将其重构为更有效的东西。 谢谢各位。
【问题讨论】:
标签: javascript jquery jquery-selectors