用数组中的对象制作表格?

时间:2016-01-03 19:29:19

标签: javascript html

我是javascript的初学者,我的代码有点问题。我找到了一个练习,我试图这样做。我必须编写一个函数,将变量中的文本插入表中。我从未见过这样的事情。此变量看起来像数组中的四个对象。当我按下按钮时,我想在表格中显示文字。有两个按钮。当我按下" Fizyka"按钮我应该看到:

  • Fizyka
  • Ola Kowal
  • Ela Nowak

当我按下" Chemia":

  • Chemia
  • Ala Goral
  • Ula Szpak

所以这是我的代码。我可以编辑的是功能展示(学习):

<!DOCTYPE html>
<html lang="pl">
 <head>

    <meta charset="utf-8">

 </head>
<body>

<button onclick="show('fizyka')">Fizyka</button>
<button onclick="show('chemia')">Chemia</button>
<div id="list"></div>

    <script>

        var student=[
            {name:"Ola", second_name:"Kowal", study:"fizyka"},
            {name:"Ela", second_name:"Nowak", study:"fizyka"},
            {name:"Ala", second_name:"Goral", study:"chemia"},
            {name:"Ula", second_name:"Szpak", study:"chemia"},
                    ];  

        function show(study)
        {
            if (study==='fizyka')
            {
            document.getElementById("list").innerHTML = "<h2>student.kierunek</h2><ul><li>student.name + " " + student.second_name</li><li>student.name + " " + student.second_name</li></ul>";

            }

            if (study==='chemia')
            {
            document.getElementById("list").innerHTML = "<h2>student.kierunek</h2><ul><li>student.name + " " + student.second_name</li><li>student.name + " " + student.second_name</li></ul>";
            }
        }

    </script>

</body>
</html>

它无法正常工作。 我不知道如何将此变量中的文字插入表格。

1 个答案:

答案 0 :(得分:1)

您的代码存在一些问题。我写了一段有效的代码,你可以使用它并激发灵感。

<button onclick="show('fizyka')">Fizyka</button> 
<button onclick="show('chemia')">Chemia</button>
<div id="list"><h2></h2><ul></ul></div>


<script>
    //Student array
    var student=[
        {name:"Ola", second_name:"Kowal", study:"fizyka"},
        {name:"Ela", second_name:"Nowak", study:"fizyka"},
        {name:"Ala", second_name:"Goral", study:"chemia"},
        {name:"Ula", second_name:"Szpak", study:"chemia"},
                ];  

    function show(study)
    {
      console.log('ENTER show('+study+')');
      //Select h2 element 
      var header = document.getElementById("list").firstChild;
      //Set h2 element text
      header.innerHTML = study;

      //Select ul element
      var list = document.getElementById("list").lastChild;
      //Set inner html to empty string to clear the content
      list.innerHTML = "";
      //loop through students and set the appropriate html element values
      for(var i = 0; i < student.length; i++){
        //check whether student[i] studies study which is put as a paramter into the function
        if(student[i].study === study){
          //Create new li element
          var li = document.createElement('li');
          //Into li element add a new text node which contains all data about the student
          li.appendChild(document.createTextNode(student[i].name + ' ' + student[i].second_name));
          //add li element into ul
          list.appendChild(li); 
        }
      }
      console.log('LEAVE show('+study+')');

    }

</script>