开发者问题收集

制作包含数组对象的表格?

2016-01-03
63

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

  • Fizyka
  • Ola Kowal
  • Ela Nowak

当我按下“Chemia”时:

  • Chemia
  • Ala Goral
  • Ula Szpak

所以这是我的代码。我能编辑的只有函数 show(study):

<!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个回答

您的代码有几个问题。我编写了一段可以运行的代码,您可以使用它并从中得到启发。

<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>
Kingmaker
2016-01-03