简体   繁体   English

将类添加到动态添加的元素

[英]Add class to dynamically added elements

I have a function that parses a JSON string and generates a grid of tiles. 我有一个解析JSON字符串并生成图块网格的函数。 The grid width and height is set in the JSON, as well as the coordinates of "special tiles" like obstacles. 在JSON中设置网格的宽度和高度,以及障碍物等“特殊图块”的坐标。

Thing is, the function that parses the JSON generates the map but doesn't add the "obstacle" class on tiles because they are dynamically added. 事实是,解析JSON的函数会生成地图,但不会在图块上添加“障碍”类,因为它们是动态添加的。

My function to parse the JSON : 我解析JSON的函数:

            $('#json-result textarea').change(function(){

                $.each($.parseJSON($(this).val()), function (item, value) {
                    if(item == 'name') { $('#name').val(value); }
                    if(item == 'width') { $('#width').val(value); }
                    if(item == 'height') { $('#height').val(value); }

                    generateMap();

                    if (item == 'obstacles') {
                        $.each(value, function (i, object) {

                            console.log('[data-x="' + object['x'] + '"][data-y="' + object['y'] + '"]');
                            var obs = $('#map-preview').find('[data-x="' + object['x'] + '"][data-y="' + object['y'] + '"]');

                            console.log(obs);
                            obs.addClass('obstacle');


                        });
                    }

                });

            });

The generateMap function is here and generates the grid. generateMap函数在此处并生成网格。 It gets values of height and width in input boxes because the process is also "reverse". 它在输入框中获取高度和宽度的值,因为该过程也是“反向”的。

function generateMap(){
    $('#map-preview').html('');

    var height = $('#height').val();
    var width = $('#width').val();

    for(h = 0 ; h < height ; h++) 
    {
          for(w = 0 ; w < width ; w++)
          {
               $('#map-preview').append('<div class="tile" data-x="' + w + '" data-y="' + h + '"></div>');
          }

          $('#map-preview').append('<br />');
    }
}

The HTML generated is inside the map-preview div. 生成的HTML位于map-preview div中。 The map-preview div is there at the beginning and is empty. map-preview div位于开头,并且为空。 The "tiles" are added dynamically. “平铺”是动态添加的。

<div id="map-preview">
    <div class="tile" data-x="0" data-y="0"></div>
    <div class="tile" data-x="1" data-y="0"></div>
    <div class="tile" data-x="2" data-y="0"></div>
    <div class="tile" data-x="3" data-y="0"></div>
    <br>
    <div class="tile" data-x="0" data-y="1"></div>
    <div class="tile" data-x="1" data-y="1"></div>
    <div class="tile" data-x="2" data-y="1"></div>
    <div class="tile" data-x="3" data-y="1"></div>
    <br>
    <div class="tile" data-x="0" data-y="2"></div>
    <div class="tile" data-x="1" data-y="2"></div>
    <div class="tile" data-x="2" data-y="2"></div>
    <div class="tile" data-x="3" data-y="2"></div>
    <br>
</div>

So, in the first snippet of code, I try to add a class to the obstacle tiles, but nothing is done. 因此,在第一段代码中,我尝试向障碍图块添加一个类,但是什么也没做。 When I try to type the same code in the Chrome console it works but not in code. 当我尝试在Chrome控制台中键入相同的代码时,它可以工作,但不能在代码中工作。

I tried with and without .find(), but nothing works. 我尝试使用和不使用.find(),但是没有任何效果。

Most likely the generateMap(); 最有可能的generateMap(); should be called outside of the .each() because now on each iteration of $.each($.parseJSON($(this).val()), function (item, value) { the map is regenerated and thus removes all previous alterations. 应该在.each()之外调用,因为现在在$.each($.parseJSON($(this).val()), function (item, value) {每次迭代中$.each($.parseJSON($(this).val()), function (item, value) {地图会重新生成,因此会删除所有先前的改变。

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

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