简体   繁体   English

Javascript动态添加HTML

[英]Javascript Dynamically added HTML

Please Look at the following code only the last image moves. 请查看以下代码,仅查看最后一张图片。 http://jsfiddle.net/u8Bg3/ http://jsfiddle.net/u8Bg3/

But second one works http://jsfiddle.net/u8Bg3/1/ 但第二个工作http://jsfiddle.net/u8Bg3/1/

As pointed by the Er144 even this works with jquery http://jsfiddle.net/u8Bg3/14/ 正如Er144指出的那样,即使这也适用于jquery http://jsfiddle.net/u8Bg3/14/

I also found out appendchild works but not innerhtml 我也发现了appendchild的作品,但没有发现innerhtml

The difference between two is that in first one html exits in second one it's dynamically created 两者之间的区别在于,第一个html退出第二个,它是动态创建的

HTML HTML

<body>
    <div class="racetrack" id="racetrack"></div>
    <div id="track-tmpl" class="hide">
        <div class="track"><div id="player{{ x }}" class="runner"></div></div>
    </div>
</body>

JS JS

var position = [0,40,80,120,80],
    racetrack = document.getElementById('racetrack');
    track_tmpl = document.getElementById('track-tmpl').innerHTML;


function Players(ele, ptimeout)
{
    this.el = ele;
    this.i = 0;
    this.iterations = 0;
    this.stop = 0;
    this.timeout = ptimeout;
    this.position = 0;

    this.animate = function(){
        if(this.i !== 0){
            this.move((this.position + 5), this.i);
        }
        if(!this.stop){
            if(this.i < 5){
                setTimeout(function(_this){             
                    _this.i++;
                    _this.animate();
                },this.timeout,this);
            }
            if(this.i==5){
                this.iterations ++;
                if(this.iterations < 50){
                    this.i = 0;
                    this.animate();
                }
                else{
                    this.el.style.backgroundPosition = '120px 0px';
                }
            }
        }
    };


    this.start = function(){
        this.stop = 0;
        this.animate();
    };

    this.move = function(to,positionIndex){
        this.position = to;
        this.el.style.backgroundPosition = '-'+position[positionIndex]+'px 0px';
        this.el.style.webkitTransform = 'translate('+to+'px)';
        this.el.style.mozTransform = 'translate('+to+'px)';
    }
}

function Game(noOfPlayers){

    this.noOfPlayers = noOfPlayers;

    this.players = new Array();

    for (var i = 0; i < this.noOfPlayers ; i++){
        racetrack.innerHTML = racetrack.innerHTML + track_tmpl.replace('{{ x }}', i);
        this.players.push(new Players(document.getElementById('player' + i), (120 + i)));
        /* issue here with dynamic added content*/
    }


    this.start = function(){
        for (var i = 0; i < this.noOfPlayers; i++){
            this.players[i].start();
        }
    };
}

var game = new Game(3);
game.start();

Why is that in dynamically added html only the last one moves 为什么在动态添加的html中只有最后一个移动

If you use the jquery: 如果你使用jquery:

var element = track_tmpl.replace('{{ x }}', i);
$(racetrack).append(element);

instead of the line where you change the innerHtml of racetrack div, all elements are moving. 而不是你改变racetrack div的innerHtml的行,所有元素都在移动。 However, I'm not sure, why... 但是,我不确定,为什么......

The issue is with creating the player(n) object inside the for loop along with the assignments to innerHTML using `+='. 问题是在for循环中创建player(n)对象以及使用`+ ='分配innerHTML。 The modified fiddle: http://jsfiddle.net/u8Bg3/15/ works fine. 修改过的小提琴: http//jsfiddle.net/u8Bg3/15/工作正常。 Cheers for a good question! 为一个好问题干杯!

  var finalized_tracks= "" ;
    for (var i = 0; i < this.noOfPlayers ; i++){
    finalized_tracks +=  track_tmpl.replace('{{ x }}', i);
    }
  racetrack.innerHTML = racetrack.innerHTML + finalized_tracks;

    for (var i = 0; i < this.noOfPlayers ; i++){
  this.players.push(new Players(document.getElementById('player'+ i),(120+i)));
    }

theCoder has pretty much nailed the issue with your code there. theCoder几乎已经解决了你的代码问题。

Just as an additional thought, you could manually build the necessary divs using javascript instead, it's more long winded however... 作为一个额外的想法,你可以使用javascript手动构建必要的div,但它更长时间缠绕...

for (var i = 0; i < this.noOfPlayers ; i++){
    var newTrack = document.createElement("div");
        newTrack.id = "track"+i;
        newTrack.className = "track";

        var newPlayer = document.createElement("div");
        newPlayer.id = "player"+i;
        newPlayer.className = "runner";

        newTrack.appendChild(newPlayer);
        racetrack.appendChild(newTrack);

        this.players.push(new Players(document.getElementById('player' + i), (120 + i)));
}

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

相关问题 html javascript 自动完成功能不适用于动态添加的文本框 - html javascript autocomplete not working with dynamically added textboxes 自动聚焦于动态添加的文本框-html / javascript - Autofocus on dynamically added textboxes - html/javascript 处理使用“香草” javascript动态添加的HTML元素 - Manipulate HTML elements added dynamically with 'vanilla' javascript 如何在javascript中匹配ID以在html上动态添加字段? - How to match id in javascript for dynamically added field on html? 如果包含控制器代码的javaScript动态添加到HTML dom中,则控制器未加载 - Controller not loading if javaScript containing controller code is added dynamically to HTML dom 如何在 JavaScript 中为 HTML 上动态添加的字段执行乘法? - How to perform multiplication in JavaScript for dynamically added field on HTML? 自定义 Javascript 库:将事件侦听器添加到动态添加的 html - Custom Javascript library: Add event listener to dynamically added html 动态添加的JavaScript会覆盖html页面视图(而不是代码) - Dynamically added JavaScript overwrite the html page view (not the code) 从javascript使用动态添加的html-object的问题 - Problem using dynamically added html-object from javascript 动态添加的javascript无法运行 - Dynamically added javascript not running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM