gpt4 book ai didi

javascript - 创建动态点击次数函数

转载 作者:行者123 更新时间:2023-12-02 16:36:58 25 4
gpt4 key购买 nike

我目前有一个功能,可以自动滚动到 Bootstrap 多选下拉列表中的事件选项。但是,我无法修改我的代码以同时处理可变数量的下拉列表。我的问题在于创建多个函数,或者让单个函数能够执行此操作。

     $(document).ready(function () {
var $dropdowns = $('fieldset').find("button.multiselect"); //Find all the dropdowns
console.log($dropdowns); //Show the ones that have been found
$.each($dropdowns, function (i) { //Just get the index of each
console.log(i); //Show index
myFunction(i); //Create n different .click functions (each with their own globals)
});

function myFunction(index) {
var not_found = true;
var jump_to = 0;
var temp = -1;
//Recursively find scrolling value in pixels, then move.
//This works for a single dropdown.
$('button.multiselect:eq(index)').click(function () {
if (not_found == true) {
$('.multiselect:eq(index)').dropdown('toggle');
temp = $('ul.multiselect-container:eq(index) li.active').position().top;
$('.multiselect:eq(index)').dropdown('toggle');
if (temp > jump_to) {
jump_to = temp;
not_found = false;
}
else if (temp <= jump_to) {
$("button.multiselect:eq(index)").click();
}
}
console.log($('ul.multiselect-container:eq(index) li.active').text());
$('ul.multiselect-container:eq(index)').animate({scrollTop: jump_to}, 750); //Scroll to active element
})
}
});

我不确定是否应该对 addEventListener() 使用一些技巧.

最佳答案

您需要做的一件事是从字符串中取出 index 并传递变量:

$('button.multiselect:eq('+index+')') 
...
$('.multiselect:eq('+index+')').dropdown('toggle');
...

对于index的所有实例

关于javascript - 创建动态点击次数函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27851115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com