简体   繁体   English

如何在Javascript中将大量变量传递给函数

[英]How to transfer a lot of variables to a function in Javascript

How can I pass many variables to a javascript function? 如何将许多变量传递给javascript函数? I would like to simplify my code. 我想简化我的代码。 It is much too long if I have to write an extra function for each variable. 如果我必须为每个变量编写一个额外的函数,这太长了。 Any help will be appreciated. 任何帮助将不胜感激。 Thank You. 谢谢。

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');;
    });
});
});

If your elements are as provided in question. 如果您的元素是有问题的。 This approach will work for you. 这种方法将为您服务。

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);;
      });
    });
  })
});

You can use data-* prefix cutsom attribute to persists arbitary data with element which can be accessed using .data() or Element.dataset property. 您可以使用data-*前缀cutsom属性来保留具有可以通过.data()Element.dataset属性访问的element的任意数据。

Assign a CSS class ie item then use Class Selector (".class") to bind event handler 分配一个CSS类,即item然后使用Class Selector (".class")绑定事件处理程序

HTML 的HTML

<div class='item' data-related-div="#div1" data-related-item="item-11">item 1</div>
<div class='item' data-related-div="#div2" data-related-item="item-12">item 2</div>
<div class='item' data-related-div="#div3" data-related-item="item-13">item 3</div>

Script 脚本

$('.item').click(function () {
    var div =  $(this).data('relatedDiv');//this.dataset.relatedDiv
    var item =  $(this).data('relatedItem');//this.dataset.relatedItem
    $('html, body').animate({
        scrollTop: $(div).offset().top
    }, 2000, function () {
        revapi8.revcallslidewithid(item);;
    });
});

You could simple use the jQuery index() method to get the clicked elements index and use the get() method to get the corresponding div element. 您可以简单地使用jQuery index()方法获取被单击元素的索引,并使用get()方法获取对应的div元素。 This will of course only work if the amount of clickable elements is equal to the div elements you want to scroll to. 当然,这仅在可单击元素的数量等于要滚动到的div元素时才有效。

Here is an example. 这是一个例子。

 var $body = $('html, body'); var $itemsWrap = $('#items-wrap'); var $items = $itemsWrap.find('.item'); var $divs = $('[id^="div"]'); $itemsWrap.on( 'click', function(evt) { var index = $items.index( evt.target ); $body.animate({ scrollTop: $($divs.get(index)).offset().top }, 666); }); 
 html, body { min-height: 100%; margin: 0; } div[id^="div"] { height: 100vh; background: tomato; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="items-wrap"> <button class="item">item-1</button> <button class="item">item-2</button> <button class="item">item-3</button> </div> <div id="div1"><h1>Div 1</h1></div> <div id="div2"><h1>Div 2</h1></div> <div id="div3"><h1>Div 3</h1></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM