如何在循环中制作这个javascript?

时间:2015-11-13 08:45:37

标签: javascript php

抱歉,我是Javascript的新手。我不知道如何在Javascript中进行循环。这是我的fiddle,这太丑了,因为那里没有循环。我用PHP制作它,但我不知道如何在javascript中制作这样的循环。这是我在PHP中的代码:

Join

我正在尝试转换为像fiddle这样的javascript,但它还没有奏效。我不知道如何在Javascript中使用循环。感谢。

这是我的javasript代码,我没有成功:

<?php
    $username='michel jackson';
    for($a=1;$a<=2;$a++){
        echo "
            <div id='bigbox'>
        ";
        for($b=1;$b<=$a;$b++){
            echo "
                <div class='colkiri'>
                    <div class='colkirichild'></div>
                </div>
            ";
        }
        echo "
            <div class='colkanan'>
                    <div class='colkananchild'>
                        <div id='username'>
                            <span class='usernamechild'><img src='http://images2.fanpop.com/images/photos/7600000/Bad-michael-jackson-7647469-1787-2560.jpg' width='15' height='15' class='bayangan'></img> $username</span> <span class='countdown'>5 minutes ago</span>
                        </div>
                        <div id='comment'>
                            <div class='commentchild'>Michael Joseph Jackson was born on August 29, 1958. He was the eighth of ten children in an African-American working-class family who lived in a two-bedroom house on Jackson Street in Gary, Indiana, an industrial city and a part of the Chicago metropolitan area.[12][13] His mother, Katherine Esther Scruse, was a devout Jehovah's Witness.</div>
                            <div id='reportthis'>
                                <span id='idreply' onclick='clickreply(this)'>reply</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        ";
    }
?>

2 个答案:

答案 0 :(得分:2)

这是你在javascript中的PHP循环。几乎相同...只需记住使用<script> javascript code here </script>标签告诉浏览器这是javascript

for (a = 1; a < 2; a++) { 
    //Your stuff inside here
}

要将HTML附加到div,您可以使用以下代码:

document.getElementById('divID').innerHTML = '<p>your data here</p>';

或者如果你也包括Jquery,它可以做得更简单:

$('#divID').append('<p> your data here </p>');

答案 1 :(得分:1)

在Javascript中:

double sum = 0.0;
for(size_t i=0, n=data.num_rows(); i<n; i+=2)
{
  sum+=double(data[i]);
}