let count = 0; // Track how many copies we have let $template = $(‘.c-input–select’); // The HTML you want to copy each time let $container = $(‘.for_select’); // Where the copies should be added $(‘#add_select’).on(‘click’, function() { // Increment our counter count++; // Create a copy of your base HTML/select let $copy = $template.clone(); // Find the select in the div, and give it an id so we can find it $copy.find(‘select’).attr(‘id’, count); // Append it $container.append($copy); // Initialise it as a select2, using the id we just gave it to find it $(‘#’ + count).select2(); });.hidden { display: none;}
注意事项
确保在初始化 Select2 之前,相关的 HTML 元素已经添加到 DOM 中。每次动态创建 Select2 控件后,都要立即对其进行初始化。使用唯一的 ID 可以避免多个 Select2 控件之间的冲突。根据实际需求,可以调整 Select2 的配置选项。
给滚动元素平滑过渡 如何在滚动条属性(scrollleft)发生改变时为元素添加平滑的过渡效果? 解决方案:scroll-behavior 属性 为滚动容器设置 scroll-behavior 属性可以实现平滑滚动。 html 代码: click the button to slide right!…