您可以取消初始化 javascript 函数吗?

Can you uninitialize a javascript function?

我正在使用 Select2 jquery 库来设计我的 select 元素。在某个媒体查询中,我想删除库并使用标准 select 元素。是否可以在某个媒体查询中取消初始化 jquery 库?

$("#location").select2({});

$(window).on("resize", function() {
  var win = $(this);

if (win.width() <= 575) {
  // uninitialize select2
}

});

在select2中你可以使用destroy解除绑定

// Destroy location
$('#location').select2('destroy');

所以在你的例子中:

$("#location").select2({});

$(window).on("resize", function() {
  var win = $(this);

if (win.width() <= 575) {
      $('#location').select2('destroy');

}

});

文档:https://select2.org/programmatic-control/methods#destroying-the-select2-control