将新数据写入javascript变量并解析JSON
我有以下代码,可帮助我在页面上显示Verite Timeline,以便:将新数据写入javascript变量并解析JSON,javascript,jquery,json,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,
<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>
因此,我有一个变量:
var place.name;
var zajson;
当我点击addtotimeline
时,我想将新元素(如上所示)添加到source:in date,作为新的值块
有可能这样做吗
以及如何更新源代码:并在添加新数据块时再次运行它
对不起,我的英语函数addContent(){
变量内容={
“startDate”:“CurrentDate+zajson”,//以及如何在不刷新页面的情况下更新此代码以在我的代码中工作但当我单击按钮调用此最后一行代码函数时如何操作?您可以只向按钮添加onclick事件,或者更好地,创建一个函数,我已更新了我的回答我尝试了:“添加到时间线”;但不起作用……但我会修复它,非常感谢……t中的语法这有点糟糕…请参阅我的答案,以获得更简洁的解决方案。您还可以扩展addContent函数以接收参数,从而使其更灵活。
var place.name;
var zajson;
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>