简体   繁体   English

如何在$(document).ready(function(){})中使用for循环?

[英]How can I use a for-loop within a $(document).ready(function(){})?

I've been trying to modify code I found here: http://www.w3schools.com/jquery/jquery_hide_show.asp to have multiple hide buttons linked with multiple paragraphs. 我一直在尝试修改在这里找到的代码: http : //www.w3schools.com/jquery/jquery_hide_show.asp,以使多个隐藏按钮与多个段落链接。 The number of paragraphs/buttons is variable--so I'd need it to work with a for-loop or while loop. 段落/按钮的数量是可变的-所以我需要它与for循环或while循环一起使用。

I've gotten it to work this far: 我已经做到了这一点:

<script>

  $(document).ready(function(){
    $('.x'+1).click(function(){ $('.file'+1).toggle(1000);});
    $('.x'+2).click(function(){ $('.file'+2).toggle(1000);});
    $('.x'+3).click(function(){ $('.file'+3).toggle(1000);});
  });


</script>
</head>
<body>


<li class="file1">This is a paragraph with little content.</li>
<button class="x1">hide</button>
<p>
<li class="file2">This is another small paragraph.</li>
<button class="x2">hide</button>
<p>
<li class="file3">This is a paragraph.</li>
<button class="x3">hide</button>
</body>
</html>

But when I try to replace the inside of that $(document).ready(function(){}) with a for-loop it quits working. 但是,当我尝试用for循环替换$(document).ready(function(){})的内部时,它退出了工作。 So, why doesn't this code below do anything? 那么,下面的这段代码为什么不做任何事情?

var m,k=3;
  $(document).ready(function(){
  for (m = 1; m <=k; m++) {
    $('.x'+m).click(function(){ $('.file'+m).toggle(1000);});
    }
  });

update to this code . 更新此代码。 DEMO DEMO

 $(document).ready(function(){
    $('button[class^="x"').click(function(){ $(this).prev('li').toggle(1000);});
 }); 

also your html is invalid so write proper HTML code so i can explain in more detail 同样你的HTML是无效的,所以写正确的HTML代码,所以我可以更详细地解释

Your html has a problem, so assuming the button & the target elements are not siblings you can 您的html有问题,因此假设按钮和目标元素不是同级,您可以

<li class="file1">This is a paragraph with little content.</li>
<button class="x" data-target=".file1">hide</button>

<li class="file2">This is another small paragraph.</li>
<button class="x" data-target=".file2">hide</button>

<li class="file3">This is a paragraph.</li>
<button class="x" data-target=".file3">hide</button>

then 然后

$(document).ready(function () {
    $('.x').click(function () {
        $($(this).data('target')).toggle(1000);
    });
});

Demo: Fiddle 演示: 小提琴

Try this using .each loop 尝试使用.each循环

$('button[class^=x]').click(function(){ 
    $(this).prev("li").toggle(1000);
});

Demo 演示

This would do in the way you planned to do it, ie using a for loop 这将按照您计划的方式进行,即使用for循环

$(document).ready(function() {
function addClickHandler(i) {
    $('.x'+i).click(function(){ $('.file'+i).toggle(1000);});
}

var j;
for(j = 1; j <=3; j++) {
    addClickHandler(j);
}
});

I would however do something like this 但是我会做这样的事情

 $(document).ready(function() { $(".file button").click(function() { $(this).prev().toggle(1000); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="file"> <p>This is file 1</p> <button>Click me</button> </div> <div class="file"> <p>This is file 2</p> <button>Click me</button> </div> <div class="file"> <p>This is file 2</p> <button>Click me</button> </div> 

You would be using fewer class names ie file1, file2 and x1, x2 etc.. 您将使用较少的类名,即file1,file2和x1,x2等。

Here are already a lot of good answers. 这里已经有很多不错的答案。 I'd like to specifically add something crucial about JavaScript. 我想特别添加一些关于JavaScript的关键信息。

The problem is not the for loop in your function , but the function definition inside your for loop. 这个问题是不是for你的循环function ,但该function的内部定义for循环。

Differ between the compile time of a function and its execution time. function的编译时间与其执行时间之间存在差异。 In your example, compile time is during the for loop, execution time is at the moment of clicking. 在您的示例中,编译时间是在for循环中,执行时间是在单击时。 How can you expect your variable m to be the same while compile time like at execution time? 在编译时(如在执行时),如何期望变量m相同? If it is not the same, how can your code work? 如果不一样,您的代码如何工作?

Once your code has left the loop, anything may happen to m , and at the clicking time, m is not valid as you expect it. 一旦代码离开循环, m可能会发生任何事情,并且在单击时, m无效,正如您所期望的那样。

This is only one example, here is more about why defining function s in loops may be puzzling: http://jslinterrors.com/dont-make-functions-within-a-loop 这只是一个例子,这里更多地说明了为什么在循环中定义function可能令人费解: http : //jslinterrors.com/dont-make-functions-within-a-loop

暂无
暂无

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

相关问题 如何将相同的$(document).ready(function()用于多个输入? - How can I use the same $(document).ready(function() for multiple inputs? 如果已经使用onload或document.ready调用了Javascript中的函数,我如何调用它? - How can i invoke a function within Javascript when its already invoked with onload or document.ready? 如何有效地使用yepnope.js和$(document).ready()? - How can I use yepnope.js with $(document).ready() effectively? 如何在文档就绪功能中使用模块文件 - How can I use module file in the document ready funciton 如何访问iframe的$(document).ready(function(){})中包含的函数? - How can I access the functions wrapped in the $(document).ready(function(){}) of an iframe? 如何将一个函数的值转换为文档准备就绪? - How can I get the value from one function into document ready? 如何在document.ready的最后调用函数 - How can I call a function at the very end of document.ready 如何在样式组件中使用for循环? - How can i use for-loop in styled-components? 如何执行一个 for-loop function,然后在 for-loop function 完成一个按钮后调用另一个 function? - How can I execute one for-loop function, then call another function after the for-loop function is complete with a button? 为什么我不能使用onClick在jQuery $(document).ready函数中执行函数? - Why can't I use onClick to execute a function inside a jQuery $(document).ready function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM