JavaScript Create Table无法在IE中运行

时间:2017-03-06 16:12:26

标签: javascript google-chrome internet-explorer firefox

请看下面的例子。我无法解释为什么它在FireFox& Chrome但不在IE 11中

https://jsfiddle.net/4qr8Ln4e/

<html>
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript">
    data = { d : {
        results: [
            { Title: 'Title1', Description: 'Description1', Status: 'Status1', Complete: 'Complete1' },
            { Title: 'Title2', Description: 'Description2', Status: 'Status2', Complete: 'Complete2' },
            { Title: 'Title3', Description: 'Description3', Status: 'Status3', Complete: 'Complete3' },
            { Title: 'Title4', Description: 'Description4', Status: 'Status4', Complete: 'Complete4' } ] } };

    data.d.results.push({Title: 'Title5', Description: 'Description5', Status: 'Status5', Complete: 'Complete5'});


    $(document).ready(function() {
        for (var i = 0; i < data.d.results.length; i++) {
            item = data.d.results[i]
            str  = '<tr><td>' + item.Title + '</td><td> ' + item.Description + '</td><td>' + item.Status + '</td><td>' + item.Complete + '</td></tr>';
            $('#mytab tr').last().after(str);
       }
    });

    </script>
  </head>
  <body>
  <table id="mytab">
    <tr>
      <td>Task Title</td> <td>Description</td> <td>Task Status</td> <td>% Complete</td>
    </tr>
  </table>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

简单的回答你是在没有var的情况下声明变量而且它可以在Firefox和Chrome上运行但在IE中却不行,这是一种不用var声明变量的不良做法,运行你的代码你只需要改变

   for (var i = 0; i < data.d.results.length; i++) {
       var item = data.d.results[i]
       var str  = '<tr><td>' + item.Title + '</td><td> ' + item.Description + '</td><td>' + item.Status + '</td><td>' + item.Complete + '</td></tr>';
        $('#mytab tr').last().after(str);
   }

答案 1 :(得分:1)

请在项目前添加var:

var item = data.d.results[i];