1

我有一个创建列表的 javascript。所有作品,看我的小提琴:http: //jsfiddle.net/mauricederegt/mjdyW/4/

现在这个脚本产生:

<div id="container">
  <ul>
    <li>
      <a href="/best-known-for">Link1</a>
    </li>
  </ul>
  <ul>
    <li>
      <a href="/life-events">Link1</a>
    </li>
  </ul>
</div>

如您所见,该<li>部分中只有 1 个超链接。我想在<li>部件中添加更多超链接,以便脚本生成如下列表:

<div id="container">
  <ul>
    <li>
      <a href="/best-known-for">Link1</a>
      <a href="/best-antoher">Some oher link</a>
      <a href="/best-antoher2">Another oher link</a>
    </li>
  </ul>
  <ul>
    <li>
      <a href="/life-events">Link1</a>
      <a href="/life-events2">Link2</a>
    </li>
  </ul>
</div>

如何编辑我的 javascript,以便从脚本中读取/创建更多超链接?

另外,我还想<li>在单个中添加更多内容<ul>,这也有点卡住了。我的最终目标必须看起来像这样:

<div id="container">
  <ul>
    <li>
      <a href="/best-known-for">Link1</a>
      <a href="/best-antoher">Some oher link</a>
      <a href="/best-antoher2">Another oher link</a>
    </li>
    <li>
      <a href="/best-known-for2">Link2</a>
      <a href="/best-antoher2">Some oher link2</a>
      <a href="/best-antoher22">Another oher link2</a>
    </li>
  </ul>
  <ul>
    <li>
      <a href="/life-events">Link1</a>
      <a href="/life-events2">Link2</a>
    </li>
  </ul>
</div>

亲切的问候

4

4 回答 4

3
var Menu = function(data) {
    this.data = data;
};

Menu.prototype.render = function() {
    //console.log(this.data)
    var ul = $("<ul />");
    $.each(this.data, function(i, e) {
        var li = $("<li />");
        $.each(e, function(j, f) {
            li.append($("<a></a>", {
                href: f.url,
                text: f.title
            }));
        });
        ul.append(li);
    });
    return ul;
};

Menu.renderMenus = function(menus, root) {
    $.each(menus, function(key, val) {
        var m = new Menu(val),
            ul = m.render();
        ul.appendTo(root);
    });
}

var menu = 
[
    [//first ul
        [ //first li 
            { //first <a> element
                title: "First UL, First li, First a",
                menuCaption: "Best Known For Caption",
                url: "/best-known-for"
            },
            { //second <a> element
                title: "First UL, First li, Second a",
                menuCaption: "Best Known For Caption",
                url: "/best-known-for"
            }
        ],
        [ //second li 
            { //only <a> element
                title: "First UL, Second li, First a (not first-child, and not red)",
                menuCaption: "Choose a life Event",
                url: "/life-events"
            }
        ]
    ],
    [//second ul
        [ //first li in second ul
            { //only <a> element
                title: "Second UL, First li, First a",
                menuCaption: "Hello Kitty",
                url: "/google"
            },
            { //second <a> element
                title: "Second UL, First li, Second a",
                menuCaption: "Best Known For Caption",
                url: "/best-known-for"
            }
        ]
    ] //you can add as many <li> or <a> elements you wish by following the 
      //pattern in this object literal
];


Menu.renderMenus(menu, $("#container"));​

小提琴

于 2012-08-16T23:09:34.620 回答
1

我稍微修改了代码以创建您想要的结构。检查评论。

var Menu = function(data) {
       this.data = (data.length) ? data : [data]; 
    };

    Menu.prototype.render = function(root) {
        // generate one li per menu item and one anchor for each element from data
        var li = $("<li></li>");
        $.each(this.data, function(link) { 
            $("<a></a>", {
                href: link.url,
                text: link.title
            }).appendTo(li);
        });
        li.appendTo($('#menuContainer'));
    };

    Menu.renderMenus = function(menus, root) {
        // create global ul here and add and id so you can point later to it
        var ul = $("<ul></ul>").attr('id','menuContainer');
        ul.appendTo(root);
        $.each(menus, function(key, val) {
            var m = new Menu(val);
            m.render(root);
        });
    }


    // modified the structure a little. you other arrays for anchors
    var menu = [
        [{
            title: "Link1",
            menuCaption: "Best Known For Caption",
            url: "/best-known-for",

        },{
            title: "Link2",
            menuCaption: "Best Kno2wn For Caption",
            url: "/best-known-for2",

        }
           ],

            {
            title: "Link1",
            menuCaption: "Choose a life Event",
            url: "/life-events",
        }


    ];

    Menu.renderMenus(menu, $("#container"));
})
于 2012-08-16T23:22:33.660 回答
1

您可以单独附加它们(如果将其合并到循环中,这也将起作用):

Menu.prototype.render = function(root) {
    var ul = $("<ul></ul>");
    var li = $("<li></li>");
    li.append($("<a></a>", {
        href: this.data.url,
        text: this.data.title
    })).appendTo(ul);

    // More links can be added into the li
    li.append($("<a></a>", {
        href: this.data.url,
        text: this.data.title
    }));

    ul.appendTo(root);
};

您还可以将li项目添加到ul相同的方式:

ul.append(li.clone());
于 2012-08-16T22:54:40.470 回答
-4
<div id="container">
  <ul>
    <li>
      <a href="/best-known-for">Link1</a>
      <a href="/best-antoher">Some oher link</a>
      <a href="/best-antoher2">Another oher link</a>
    </li>
    <li>
      <a href="/best-known-for2">Link2</a>
      <a href="/best-antoher2">Some oher link2</a>
      <a href="/best-antoher22">Another oher link2</a>
    </li>
    <li>
      <a href="/life-events">Link1</a>
      <a href="/life-events2">Link2</a>
    </li>
  </ul>
</div> 

那会奏效吗?

于 2012-08-16T22:43:17.640 回答