简体   繁体   English

document.ready函数中的多个函数

[英]multiple functions in the document.ready function

here's my code: 这是我的代码:

$(document).ready(function () {

    $('.flip1').click(function () {
        $('.panel1').slideToggle("slow");
    });
    $('.flip2').click(function () {
        $('.panel2').slideToggle("slow");
    });
    $('.flip3').click(function () {
        $('.panel3').slideToggle("slow");
    });
    $('.flip4').click(function () {
        $('.panel4').slideToggle("slow");
    });
});

I want to make a loop with .flip as the variable (flipVar) and .panel as (panelVar) 我想用.flip作为变量(flipVar)和.panel作为(panelVar)进行循环

Well if it were my page I'd make sure that those elements all shared a class so that I wouldn't need to loop. 好吧,如果它是我的页面,我会确保这些元素都共享一个类,这样我就不需要循环了。 However, you could do this: 但是,你可以这样做:

 for (var i = 1; i <= 4; ++i) $('.flip' + i).click((function(i) {
   return function() { $('.panel' + i).slideToggle('slow'); };
 })(i));

The loop variable has to be trapped in a closure so that each "click" handler references the proper value. 循环变量必须被捕获在闭包中,以便每个“click”处理程序引用正确的值。 Again, I really wouldn't do it this way. 再一次,我真的不会这样做。 I'd make the "flip" elements share a class, and then keep that index (the implicit reference to a corresponding "panel") in a separate class element or in a "data-" attribute. 我让“翻转”元素共享一个类,然后将该索引(对应“面板”的隐式引用)保存在单独的类元素或“data-”属性中。 Then the handler could find the panel using that value. 然后处理程序可以使用该值找到面板。

edit — as a hack, you could leverage the fact that the class names of the related elements are both of the form "somethingNN", where "NN" is the numeric part. 编辑 - 作为一个黑客,你可以利用相关元素的类名都是“somethingNN”形式的事实,其中“NN”是数字部分。 You could strip off the number and then append it to "panel": 您可以剥离该号码,然后将其附加到“面板”:

for (var i = 1; i <= 4; ++i) $('.flip' + i).click(function() {
  var panelClass = this.className.replace(/.*\bflip(\d+).*/, "panel$1");
  $(panelClass).slideToggle('slow');
});

Even though you want to run the selector in a loop, I wouldn't do it like that because you're doing multiple DOM selections. 即使你想在循环中运行选择器,我也不会这样做,因为你正在做多个DOM选择。 You can get it done with one DOM selection: 您可以使用一个DOM选择完成它:

$(document).ready(function () {
    $('div[class^=flip]').each(function ( idx ) {
        $(this).click(function() {
            $('.panel' + (idx + 1)).slideToggle("slow");
        });
    });
});

This will work assuming that the flip elements occur on the page in their numerical order. 假设flip元素以数字顺序出现在页面上,这将起作用。

This uses the attribute starts with selector to get all <div> elements that have a class name starting with "flip" . 这使用属性starts with selector来获取所有具有以"flip"开头的类名的<div>元素。 Change the tag name if it is different, or remove it if they don't all have the same tag. 如果标签名称不同,请更改标签名称,如果标签名称不同,则将其删除。

It uses the index that .each() makes available to you in order to toggle the correct .panel . 它使用.each()为您提供的index ,以便切换正确的.panel

$(document).ready(function () {

    for (var i=1; i<=4; i++) {
       (function(i) {
          $('.flip'+i).click(function () {
             $('.panel'+i).slideToggle("slow");
          });
       })(i);
    }

}); 

try this: 尝试这个:

$(function(){
    for(var i=1;i<5;i++){
         $('.flip'+i).click(function () {
             $('.panel'+i).slideToggle("slow");
         });
    }
});

PS:- don't use this it will not work as pointed out by @patrick PS: - 不要使用它,它将无法正常工作@patrick指出

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

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