对象undefined / HTMLElement分别使用jQuery输出

时间:2015-01-03 13:48:48

标签: javascript jquery json parsing getjson

我正在使用jQuery的each / getJSON来遍历我的data.json文件,抓取并格式化数据,并将其输出到#output div内的页面上。

它按预期工作,除了[object HTMLElement]奇怪地在渲染的HTML之前。

$.getJSON( 'json/data.json', function ( data ) {
    var output;
    $.each( data, function ( index, entry ) {
        output += '<div class="entry" id="' + entry.date + '">';
        $.each( entry.exercises, function ( index, exercise ) {
            var i = 1;
            output += '<table class="exercise"><tr><th>' + exercise.name 
                   + '</th><th>Weight</th><th>Reps</th></tr>';
            $.each( exercise.stats, function ( index, stat ) {
                output += '<tr><td>SET ' + i++ + '</td><td><input type="number" placeholder="' 
                   + stat.weight + '"></td><td><input type="number" placeholder="' 
                   + stat.reps + '"></td></tr>';
            });
            output += '</table>';
        });
        output += '</div>';
    }); // each
    $( '#output' ).html( output );
}); // getJSON

data.json

[
    {
        "date" : 12252014,
        "exercises" : [
            {
                "name" : "Squats",
                "stats" : [
                    { "weight" : 135, "reps" : 5 },
                    { "weight" : 225, "reps" : 10 },
                    { "weight" : 315, "reps" : 15 }
                ]
            },
            {
                "name" : "Bench",
                "stats" : [
                    { "weight" : 435, "reps" : 20 },
                    { "weight" : 525, "reps" : 15 },
                    { "weight" : 615, "reps" : 30 }
                ]
            },
            {
                "name" : "Rows",
                "stats" : [
                    { "weight" : 735, "reps" : 35 },
                    { "weight" : 825, "reps" : 40 },
                    { "weight" : 915, "reps" : 45 }
                ]
            }
        ]

    }
]

HTML错误:

<div id="output">
    "[object HTMLElement]"
    <div class="entry" id="12252014" style="display: none;">
        <table class="exercise">
            <tbody>
                <tr>
                    <th>Squats</th>
                    <th>Weight</th>
                    <th>Reps</th>
                </tr>
                <tr>
                    <td>SET 1</td>
                    <td><input type="number" placeholder="135"></td>
                    <td><input type="number" placeholder="5"></td>
                </tr>
                <tr>
                    <td>SET 2</td>
                    <td><input type="number" placeholder="225"></td>
                    <td><input type="number" placeholder="10"></td>
                </tr>
                <tr>
                    <td>SET 3</td>
                    <td><input type="number" placeholder="315"></td>
                    <td><input type="number" placeholder="15"></td>
                </tr>
            </tbody>
        </table>
        [...]
    </div>
</div>

为什么[object HTMLElement]被添加到输出中?

2 个答案:

答案 0 :(得分:3)

更改

var output;

var output = '';

这将解决您的问题。 HTML文档中可能有一个id =“output”的元素,默认情况下,带有id的元素会显示为同名的全局变量,因此<a id="hello">text</a>将显示为window.hello

答案 1 :(得分:3)

这对我来说看起来像变量output之前已经使用过并且填充了html元素。
在你的情况下,它是你的包装器div,其id为output,它作为全局变量公开。

您应该能够通过给出输出初始值来克服这一点:

var output = ''; 

因为使用var不会清除现有变量:

&#13;
&#13;
var output = 'Lorem ipsum';
var output;

alert(output)
&#13;
&#13;
&#13;