javascript文档元素由id

时间:2016-05-19 17:59:13

标签: javascript

如果我需要在页面中看到我的输出而不是在console.log中我想使用文件获取Element By Id我该怎么做?他给了我一个结果,史蒂夫只是!!

[代码:]  http://i.stack.imgur.com/ISqzT.png

<script>
    var friends = {};
    friends.bill = {
      firstName: "Bill",
      lastName: "Gates",
      number: "(206) 555-5555",
      address: ['Microsoft Way']
      };
    friends.steve = {
      firstName: "Steve",
      lastName: "Jobes",
      number: "(444) 111 000",
      address: ["Apple way"]
    };
    var list = function(obj) {
      for( var key in obj){
        console.log(obj);
      document.getElementById("demo").innerHTML = key + "<br>";
      }
    }

    var search = function(name) {
      for(var key in friends){
        if(name === friends[key].firstName){
          console.log(friends[key]);
        }
      }
    }
    list(friends);
    // search("Steve");
  </script>

2 个答案:

答案 0 :(得分:1)

嘿,你必须做的唯一改变就是使用+ =代替just = for innerHTML函数。你走了:

text.innerHTML + = friend +“
”;

答案 1 :(得分:0)

只需将结果附加到innerHTML,不要覆盖它,同样: document.getElementById("demo").innerHTML += key + "<br>";