小编Nee*_*rya的帖子

无法在javascript里面获取元素id for循环

我试图在循环中使用getElementById()作为我想要得到的每个div的id顺序如total_comments1,total_comments2,total_comments3,..等等.这个id由输出使用php分配.

这是我显示我的div的代码:

<?php
    $i=0;
    while( $row = mysqli_fetch_assoc($ret) ):
        $i++;
    ?>
        <tr>
            <td class="col3 col">
                <div id="<?php echo "total_comments".$i ?>"></div>
            </td>
        </tr>
Run Code Online (Sandbox Code Playgroud)

这是我的javascript代码:

<script>
    for(var i in (result.response)) {
        var a=document.getElementById("total_comments" + i );
        a.innerHTML = result.response[i].posts;
    }
</script>
Run Code Online (Sandbox Code Playgroud)

当我在document.getElementBy ID中仅使用"total_posts"时,它不会给出任何错误.但是当我使用"total_posts"+ i时,它会在下一行给出错误.无法设置null的属性'innerHTML'

html javascript php jquery

3
推荐指数
1
解决办法
438
查看次数

标签 统计

html ×1

javascript ×1

jquery ×1

php ×1