简体   繁体   English

修改子div的innerHTML

[英]Modify innerHTML of a child div

I am new to JavaScript. 我是JavaScript新手。 What I am trying to do is make a div and inside of it there will be another div. 我正在尝试做的是使一个div,并且在其中将有另一个div。 Within my script code I am trying to create new instances of that div using factory function if that is the right name for it, and then change the innerHTML of the child div if that is possible. 在我的脚本代码中,如果可以的话,我尝试使用工厂函数创建该div的新实例,如果可能的话,然后更改子div的innerHTML。 Thanks in advance. 提前致谢。

<div class = "loopBlock"  style="width:350px;">
    <fieldset>
        <legend style="color:black;font-weight:bold;">While Loop</legend>
        <table>
            <tr>
            <td>Condition:</td>
            <td><input type="text" /></td>
            </tr>
        </table>

        <div class = "codeDivClass" id = "codeDiv">
            HelloWorld!
        </div>
    </fieldset>
</div>


<script>
    var loopDiv = document.getElementsByClassName("loopBlock");
    var loopi =1;

    function loopObject(){
        var loopDivObject = document.createElement("div");
        loopDivObject.innerHTML = loopDiv[0].innerHTML;
        loopDivObject.className = "loopBlock";
        loopDivObject.id = "loopBlock"+loopi;
        loopi++;    
        return loopDivObject;
    };

    var functionCodeDiv = document.getElementById("codeDiv");

    for (i=0; i<5; i++){
        var tempLoop = loopObject();
        functionCodeDiv.appendChild(tempLoop);
        var id = "loopBlock"+i+1;
        document.getElementById(id).getElementsByTagName('div')[0].innerHTML = "bye";
    }

</script>

Didn't really get how it should work, but I'm sure I've found a mistake. 并没有真正了解它应该如何工作,但是我敢肯定我发现了一个错误。

    var id = "loopBlock"+i+1;

you have to replace with: 您必须替换为:

   var id = "loopBlock"+(i+1);

Example i is 2. 例子我是2。

In first case you get: "loopBlock21" 在第一种情况下,您得到:“ loopBlock21”

In second (my) case , you'll get "loopBlock3" 在第二种情况下 ,您将获得“ loopBlock3”

The problem is in operator precedence . 问题出在运算符优先级上 Since in this line 由于在这一行

var id = "loopBlock" + i + 1;

you have two + (unary plus) operators with the same precedence they will act as a string concatenation operators, because one of the operands is a string ("loopBlock"). 您有两个具有相同优先级的+ (一元加)运算符,它们将充当字符串连接运算符,因为其中一个操作数是字符串(“ loopBlock”)。

In your case you want to group i + 1 with parentheses to make the expression evaluate first as arithmetic addition operator. 在您的情况下,您希望用括号将i + 1分组,以使表达式首先作为算术加法运算符求值。 After that string concatenation with "loopBlock" will produce expected result: 在使用"loopBlock"字符串连接之后,将产生预期的结果:

var id = "loopBlock" + (i + 1);

Demo: http://jsfiddle.net/0091n9tt/ 演示: http //jsfiddle.net/0091n9tt/

I think you're problem in is this line: 我认为您的问题所在是:

document.getElementById(id).getElementsByTagName('div')[0].innerHTML = "bye";

What you are actually doing is trying to gett divs inside the newly created div (loopBlock), which is empty. 您实际上正在做的是尝试在新创建的div(loopBlock)中获取div,该div为空。

You already have a reference to the block you want to modify the innerHTML; 您已经有了要修改innerHTML的块的引用; you can simply use it like this: 您可以像这样简单地使用它:

tempLoop.innerHTML = "bye";

So you're for loop would look like this: 因此,您的for循环如下所示:

for (i=0; i<5; i++){
    var tempLoop = loopObject();
    functionCodeDiv.appendChild(tempLoop);
    tempLoop.innerHTML = "bye";
}

Note that you don't need the id anymore. 请注意,您不再需要该ID。

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

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