-3
var dates = [
                {
                    "marker": "1986", 
                    "type": "default",
                    "title": "Master of Puppets" ,
                    "content": "Metallica's third studio album, Master of Puppets, was recorded at Sweet Silence Studios and was released in March 1986. The album reached number 29 on the Billboard 200, and spent 72 weeks on the chart.[23] The album was the band's first to be certified gold on November 4, 1986, and was certified six times platinum in 2003.[24] Steve Huey of Allmusic considered the album \"the band's greatest achievement\".[25] Following the release of the album, Metallica supported Ozzy Osbourne for a United States tour.[21] Hetfield broke his wrist skateboarding down a hill and continued the tour performing vocals, with guitar technician John Marshall playing rhythm guitar.[26]"
                },
                {
                    "marker": "1991", 
                    "type": "youtube-video",
                    "title": "Ten" ,
                    "youtubeId": "VbhsYC4gKy4" ,
                    "content": "With the success of Ten, Pearl Jam became a key member of the Seattle grunge explosion."
                },
                {
                    "marker": "1992", 
                    "type": "image",
                    "title": "Nirvana",
                    "img": "http://upload.wikimedia.org/wikipedia/commons/1/19/Nirvana_around_1992.jpg",
                    "content": "Kurt Cobain (front) and Krist Novoselic (left) live at the 1992"
                },
                {
                    "marker": "1994", 
                    "type": "default",
                    "title": "5 de Abril" ,
                    "content":"<p>I am the best </p>"
                }
            ];

我的代码中有一个像这样的对象(在我的 html 页面上生成时间线)。如何动态生成这样的对象?如何动态地将内容分配给这些标签(“marker”、“type”)?

4

2 回答 2

1

要以 JSON 格式输出任何 PHP 变量,请使用json_encode.

下次,问你的意思,而不是让我们在评论中哄骗你,好吗?;)

于 2013-02-10T19:44:37.633 回答
0

JSON 代表 JavaScript 对象表示法。在 JavaScript 中,它用于定义 javascript 对象。从 JSON 构建的 JavaScript 对象基本上是键值对的集合。

由于 JavaScript 是一种基于原型的语言(请参见此处:http ://en.wikipedia.org/wiki/Prototype-based_programming ),您可以“动态生成”这种对象,只需为其分配所需的任何 key:pair 值. 例子 :

var RandomObject = {} //Create an empty key:pair values object
RandomObject["SomeKey"] = "SomeValue";

这基本上会做同样的事情,就好像你这样做了:

var RandomObject = {"SomeKey":"SomeValue"}

现在,您在这里拥有的是一组键:值对。为了动态地重新创建它,您需要首先创建一个 JavaScript 数组,然后为其分配您的 key:pair 值:

var DynamicDates = new Array();
DynamicDates[0] = {
    "marker": "1991", 
    "type": "youtube-video",
    "title": "Ten" ,
    "youtubeId": "VbhsYC4gKy4" ,
    "content": "With the success of Ten, Pearl Jam became a key member of the Seattle grunge explosion."
};

注意:我选择了第二个条目以使其在示例中更短。

于 2013-02-10T19:53:43.407 回答