JS(document.getElementById(“example”)。textContent)不显示var

时间:2016-11-22 14:34:49

标签: javascript getelementbyid

当我尝试使用id load2的内容​​显示实时图表时 当我将textContent放在控制台日志中时,它不会返回任何内容 它确实显示数字我做错了什么?

$(function () {
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });

    Highcharts.chart('container', {
        chart: {
            type: 'spline',
            animation: Highcharts.svg, // don't animate in old IE
            marginRight: 10,
            events: {
                load: function () {

                    // set up the updating of the chart each second

                    var series = this.series[0];
                    setInterval(function () {
                        var x = (new Date()).getTime(); // current time
                         var   y =  (document.getElementById("load2").textContent);
                        console.log(document.getElementById("load2").textContent);
                        series.addPoint([x, y], true, true);
                    }, 1000);
                }
            }
        },
        title: {
            text: 'Live random data'
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 150
        },
        yAxis: {
            title: {
                text: 'Value'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function () {
                return '<b>' + this.series.name + '</b><br/>' +
                    Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                    Highcharts.numberFormat(this.y, 2);
            }
        },
        legend: {
            enabled: false
        },
        exporting: {
            enabled: false
        },
        series: [{
            name: 'Random data',
            data: (function () {
                // generate an array of random data
                var data = [],
                    time = (new Date()).getTime(),
                    i;

                for (i = -50; i <= 0; i += 1) {
                    data.push({
                        x: time + i * 1000,
                        y: 0
                    });
                }
                return data;
            }())
        }]
    });
    });
});

此load2元素来自JSON拉取请求

1 个答案:

答案 0 :(得分:1)

highcharts验证addPoint中的参数类型并解除y,因为它是一个字符串而不是int或float

使用parseFloatParseInt

parseFloat(的document.getElementById( “LOAD2”)的textContent。);