如何使用javascript for循环生成每个类?

时间:2017-10-13 06:49:31

标签: javascript html for-loop

我试图使用javascript函数动态生成几个相同的类(名为"盒装"类)。这就是我尝试过的。我想用给定的数字创建相同的类(名为"盒装"类)。该数字将是我的csv文件中的列数。以下是结果和示例代码的快照。 enter image description here

<script>
function addClass(var n) {
    var myClass = document.getElementById("boxed")
    for (var i = 0; i< n; i ++){

    }   
}
</script>

//

<style type="text/css">
 table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

.boxed{
border: 1px solid blue;
padding: 10px
}
</style>



<div class="boxed">
  <p>${titile}</p><br>
  <p>${image}</p><br>
  <table class="grid" cellspacing="10">
   <tr>
     <td></td>
     <td>Strongly<br>Disagree</td>
     <td>Disagree</td>
     <td>Neither agree<br/>nor disagree</td>
     <td>&nbsp;&nbsp;&nbsp;Agree&nbsp;&nbsp;&nbsp;</td>
     <td>&nbsp;Strongly&nbsp;<br>Agree</td>
   </tr>
   <tr>
     <td class="griditem">survey1</td>
     <td><input type=radio  name="survey1" value="-2">-2</td>
     <td><input type=radio  name="survey1" value="-1">-1</td>
     <td><input type=radio  name="survey1" value="0">0</td>
     <td><input type=radio  name="survey1" value="1">1</td>
     <td><input type=radio  name="survey1" value="2">2</td>
   </tr>
   <tr>
     <td class="griditem">survey2</td>
     <td><input type=radio  name="survey2" value="-2">-2</td>
     <td><input type=radio  name="survey2" value="-1">-1</td>
     <td><input type=radio  name="survey2" value="0">0</td>
     <td><input type=radio  name="survey2" value="1">1</td>
     <td><input type=radio  name="survey2" value="2">2</td>
   </tr>
   <tr>
     <td class="griditem">survey3</td>
     <td><input type=radio  name="survey3" value="-2">-2</td>
     <td><input type=radio  name="survey3" value="-1">-1</td>
     <td><input type=radio  name="survey3" value="0">0</td>
     <td><input type=radio  name="survey3" value="1">1</td>
     <td><input type=radio  name="survey3" value="2">2</td>
   </tr>
  </table>
</div>

4 个答案:

答案 0 :(得分:0)

你必须通过给出值来调用你的函数,在函数中调用n。这不是你试图寻求的可能,如果你解释更多,我可以为你解决它。这个循环工作顺便说一句

&#13;
&#13;
function addClass(n) {
    for (var i = 0; i< n; i ++){
		console.log('countet '+i+' times');
    }   
}
window.addClass(100);
&#13;
 table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

.boxed{
border: 1px solid blue;
padding: 10px
}
&#13;
<div class="boxed">
  <p>${titile}</p><br>
  <p>${image}</p><br>
  <table class="grid" cellspacing="10">
   <tr>
     <td></td>
     <td>Strongly<br>Disagree</td>
     <td>Disagree</td>
     <td>Neither agree<br/>nor disagree</td>
     <td>&nbsp;&nbsp;&nbsp;Agree&nbsp;&nbsp;&nbsp;</td>
     <td>&nbsp;Strongly&nbsp;<br>Agree</td>
   </tr>
   <tr>
     <td class="griditem">survey1</td>
     <td><input type=radio  name="survey1" value="-2">-2</td>
     <td><input type=radio  name="survey1" value="-1">-1</td>
     <td><input type=radio  name="survey1" value="0">0</td>
     <td><input type=radio  name="survey1" value="1">1</td>
     <td><input type=radio  name="survey1" value="2">2</td>
   </tr>
   <tr>
     <td class="griditem">survey2</td>
     <td><input type=radio  name="survey2" value="-2">-2</td>
     <td><input type=radio  name="survey2" value="-1">-1</td>
     <td><input type=radio  name="survey2" value="0">0</td>
     <td><input type=radio  name="survey2" value="1">1</td>
     <td><input type=radio  name="survey2" value="2">2</td>
   </tr>
   <tr>
     <td class="griditem">survey3</td>
     <td><input type=radio  name="survey3" value="-2">-2</td>
     <td><input type=radio  name="survey3" value="-1">-1</td>
     <td><input type=radio  name="survey3" value="0">0</td>
     <td><input type=radio  name="survey3" value="1">1</td>
     <td><input type=radio  name="survey3" value="2">2</td>
   </tr>
  </table>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

&#13;
&#13;
e.g. ALTER TABLE person ADD age INT(3);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

如果你使用jquery,你正在使用JavaScript,这将真正简化你的工作。它内置了添加类功能,您可以简单地使用此功能。您不必自己创建它。

答案 3 :(得分:-1)

我不清楚你要做什么,但我觉得你正在拿一个模板并将迭代附加到类名中。如果是这样,那么你的语法是调用getElementById但你查询类名。

也许试试

getElementsByClassName("boxed")