简体   繁体   English

第二次调用已定义的函数不起作用

[英]Second call to a defined function doesn't work

This is my first time posting here, although I've browsing for a long time. 这是我第一次在这里发布信息,尽管我已经浏览了很长时间。 Saying that, I hope not to breach any regulations etc. Anyway, ad rem: 话虽如此,我希望不要违反任何法规等。无论如何,请注意:

I'm writing this piece of jQuery code, which populates the wrapper div with a lot of small divs, creating a grid. 我正在编写这段jQuery代码,其中用许多小div填充了包装div,并创建了一个网格。 It all works fine at the beginning, I created a function for that and the first call to it works fine. 在一开始它一切都很好,我为此创建了一个函数,并且对其的第一个调用也很好。 But then, when I use a button with a click() event handler, I want to call the function again and re-populate the grid with divs of different size - this is where it stops working. 但是,当我使用带有click()事件处理程序的按钮时,我想再次调用该函数,并使用不同大小的div重新填充网格-这是它停止工作的地方。 Here's my code: 这是我的代码:

 $(document).ready(function () {

      var size = 16;
      var def_width = 960;
      var def_height = 640;

      var populate = function (size) {
          $('.square').empty();
          for (var i = 0; i < size * size; i++) {
              var $sq = $("<div class='square'></div>");
              $('.wrapper').append($sq)
          };
          return $('.wrapper')
      };

      populate(size);

      $('.square').hover(function () {
          $(this).css('background-color', 'black');
      }, function () {
          $(this).css('background-color', 'white');
      });

      $('#reset').click(function () {
          var new_size = prompt('Please enter a new amount of squares');
          var new_width = def_width / new_size;
          var new_height = def_height / new_size;

          $('.square').css('width', new_width + 'px');
          $('.square').css('height', new_height + 'px');
          populate(new_size);
      });

The second call to the populate() function doesn't seem to work - the grid still has 256 squares, which are smaller, so they do not fill the grid completely. 对populate()函数的第二次调用似乎不起作用-网格仍然具有256个较小的正方形,因此它们无法完全填充网格。

You are emptying the wrong element, it should be $('.wrapper').empty(); 您正在清空错误的元素,应该是$('.wrapper').empty(); not $('.square').empty(); 不是$('.square').empty();

If you empty the square divs, then it will kill all element within the square divs which are empty to begin with but it does not remove them. 如果清空正方形div,那么它将杀死正方形div中所有以空开头的元素,但不会删除它们。 Then you proceed to add more squares. 然后,您可以添加更多的正方形。 If you run empty on the wrapper then it will remove all the pre-existing square divs in wrapper and you can add your new square divs 如果在包装器上运行为空,则它将删除包装器中所有先前存在的div,您可以添加新的div

  var populate = function (size) {
      $('.wrapper').empty();
      for (var i = 0; i < size * size; i++) {
          var $sq = $("<div class='square'></div>");
          $('.wrapper').append($sq)
      };
      return $('.wrapper')
  };

Not sure why you are returning $('.wrapper') in this function but I left it in there in case there are other codes using it 不知道为什么要在此函数中返回$('.wrapper') ,但是如果有其他代码正在使用它,我会把它留在那里

Okay, I was under the impression that empty() removes all the elements with that selector. 好的,我给人的印象是empty()使用该选择器删除了所有元素。 So after I changed it to .square, it leaves me with a completely empty wrapper div. 因此,在将其更改为.square之后,它给了我一个完全空的包装div。 Before, it changed the size of squares, so that they didn't fill the grid, buy they were still in there. 以前,它更改了正方形的大小,以使它们不会填满网格,而要购买它们仍在其中。 Right now it goes like this: I start with a 16x16 grid of divs. 现在它是这样的:我从一个16x16的div网格开始。 I need to change that, when the user inputs a new amount of squares in the side, so that it is 64x64 for example. 当用户在侧面输入新的正方形量时,我需要更改它,例如为64x64。

I wasn't sure about returning wrapper also, I added that when I was still fighting to get that function to work. 我不确定是否还要返回包装器,我补充说,当我仍在努力使该功能正常工作时。 It seemed to work, so I left it there. 它似乎有效,所以我把它留在那里。 I'm new to jQuery and JS in general, so there might be other mistakes, but right now I need to focus on the desired result. 一般而言,我是jQuery和JS的新手,因此可能还会出现其他错误,但是现在我需要关注预期的结果。 Thanks for quick answers by the way! 顺便感谢您的快速解答!

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

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