将新数据写入javascript变量并解析JSON

时间:2013-09-22 23:26:42

标签: javascript jquery json

我有这段代码可以帮助我在我的页面上显示Verite Timeline:

<div id="timeline-embed"></div>
      <script type="text/javascript">
        var timeline_config = {
         width: "100%",
         height: "100%",
         debug: true,
         rows: 2,
         source: {
    "timeline":
    {
        "headline":"Sh*t People Say",
        "type":"default",
        "text":"People say stuff",
        "startDate":"10/4/2011 15:02:00",
        "date": [
            {
                "startDate":"10/4/2011 15:10:00",
                "endDate":"10/4/2011 15:55:00",
                "headline":"FIRST",
                "text":"<p>FIRSTTEXT</p>",
                "asset":
                {

                    "caption":"yessss"
                }
            },
            {
                "startDate":"10/4/2011 17:02:00",
                "endDate":"10/4/2011 18:02:00",
                "headline":"SECOND",
                "text":"<p>In true political fashion, his character rattles off common jargon heard from people running for office.</p>",
                "asset":
                {
                    "media":"http://youtu.be/u4XpeU9erbg",
                    "credit":"",
                    "caption":""
                }
            }
        ]
    }
}

        }
      </script>

所以现在source:进入date我要添加新元素:

{
                "startDate":"CurrentDate + zajson",
                "endDate":"10/4/2011 18:02:00",
                "headline":"place.name",

                "asset":
                {
                    "media":"http://youtu.be/u4XpeU9erbg",
                    "credit":"",
                    "caption":""
                }
            }

所以offcource我有一个变量:

var place.name;
var zajson;

当我点击<button>Add to timeline</button>时,我想在源中添加新元素(如上所示):在日期,作为新的值块......

这样做是否可行?

我如何更新源代码:当我添加新的数据块时再次运行它

抱歉我的英文

1 个答案:

答案 0 :(得分:0)

function addContent() {
    var content = {
        "startDate":"CurrentDate + zajson", // <-- these two probably shouldn't be in quotes, but concated
        "endDate":"10/4/2011 18:02:00",
        "headline":"place.name", // <-- this one, too
        "asset":
            {
                "media":"http://youtu.be/u4XpeU9erbg",
                "credit":"",
                "caption":""
            }
    };

    timeline_config.source.timeline.date.push(content);
}

这样的事情应该这样做。

然后,按钮:

<button onclick="addContent()">Add to timeline</button>