gpt4 book ai didi

javascript - 如何将大量变量传递给 Javascript 中的函数

转载 作者:行者123 更新时间:2023-11-30 09:39:58 26 4
gpt4 key购买 nike

如何将多个变量传递给 javascript 函数?我想简化我的代码。如果我必须为每个变量编写一个额外的函数,那就太长了。任何帮助将不胜感激。谢谢。

jQuery(document).ready(function ($) {

$('#item1').click(function () {
$('html, body').animate({
scrollTop: $("#div1").offset().top
}, 2000, function () {
revapi8.revcallslidewithid('item-11');;
});
});

$('#item2').click(function () {
$('html, body').animate({
scrollTop: $("#div2").offset().top
}, 2000, function () {
revapi8.revcallslidewithid('item-12');;
});
});

$('#item3').click(function () {
$('html, body').animate({
scrollTop: $("#div3").offset().top
}, 2000, function () {
revapi8.revcallslidewithid('item-13');;
});
});
});

最佳答案

如果您的元素与所提供的有问题。这种方法适合您。

jQuery(document).ready(function($) {
var arr = [1,2,3]; //element iterator
arr.forEach(function(item){
$('#item' + item).click(function() {
$('html, body').animate({
scrollTop: $("#div" + item).offset().top
}, 2000, function() {
revapi8.revcallslidewithid('item-1' + item);;
});
});
})
});

关于javascript - 如何将大量变量传递给 Javascript 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41607828/

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