简体   繁体   English

使用javascript将参数传递给另一个函数(this)

[英]Pass argument to another function with javascript (this)

I think that my element in renderGrid() contains an html element so I wanna replace "#container" with this. 我认为我在renderGrid()中的元素包含一个html元素,因此我想以此替换“ #container”。 Simply I wanna make my code universal because now I can only use it for "#container" so "#container" has to be replaced with something like template. 我只是想使我的代码通用,因为现在我只能将其用于“ #container”,因此“ #container”必须替换为模板之类的东西。

 function renderGrid(element, settings) { var x = settings.columns; var y = result.length / x; $('.grid').remove(); for (var cols = 0; cols < x; cols++) { for (var rows = 0; rows < y; rows++) { numberOfTiles = x * y; $('#container').append("<div class='grid'><div class = 'usernameSpace'></div></div>"); }; }; $('.grid').width(800 / x); $('.grid').height(800 / x); } (function ($) { $.fn.tiles = function (options) { var settings = getOptions(options); return this.each(function () { var grid = renderGrid(this, options); fillTilesWithContent(this, options); colorTiles(this, options); columnCountSet(this,options); return grid; }); } })(jQuery); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Maybe place your related methods into a class and set a class variable, for example 例如,也许将您的相关方法放在一个类中并设置一个类变量

Class GridRenderer {
  var _container;
  function call(element, settings) {
    _container = element;
    renderGrid(settings);
  }
  function createGrid(x, y) {
    $('.grid').remove();
    for (var cols = 0; cols < x; cols++) {
      for (var rows = 0; rows < y; rows++) {
        numberOfTiles = x * y;
        _container.append("<div class='grid'><div class = 'usernameSpace'></div></div>");
      };
    };
    $('.grid').width(800 / x);
    $('.grid').height(800 / x);
  };

  function refreshGrid(x, y) {
    createGrid(x, y);
  };

  function renderGrid(settings) {
    var x = settings.columns;
    var y = result.length / x;
    refreshGrid(x, y);
  }
}

Then 然后

var grid = GridRenderer.call(this, options);

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

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