如何在javascript中动态设置对象的值?

时间:2017-03-20 21:15:24

标签: javascript dynamic

<script>' +

        'var doughnutData = [' +
        '{' +
        ' value: percentage,' +
//not work with this variable but with a number like 50 it work
        ' color:"#68dff0"' +``
        '},' +
        '{' +
        'value : remain,' +
//not work with this variable but with a number like 50 it work
        'color : "#444c57"' +
        '}' +
        ' ];' +
        'var myDoughnut = new Chart(document.getElementById("serverstatus0' + i + '").getContext("2d")).Doughnut(x);' +
        ' </script>' +

我有这个脚本用于创建图表,我想为它的创建动态分配值但是如果我只将我的'percentage'变量赋值给值:。 我如何才能达到目的?

1 个答案:

答案 0 :(得分:0)

百分比是变量吗?

如果是这样的话,那就是:

&#39; +

    'var doughnutData = [' +
    '{' +
    ' value: ' + percentage + ',' +
    ' color:"#68dff0"' +``
    '},' +
    '{' +
    'value : '+ remain + ',' +
    'color : "#444c57"' +
    '}' +
    ' ];' +
    'var myDoughnut = new Chart(document.getElementById("serverstatus0' + i + '").getContext("2d")).Doughnut(x);' +
    ' </script>' +

看看我如何将剩余和百分比文本更改为不在引号中?