如何在我的javascript生成列表中的<li>部分中放置更多超链接

How to put more hyperlinks in the <li> part in my javascript generated list

本文关键字:超链接 li javascript 列表 我的      更新时间:2023-09-26

我有一个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,以便它从脚本读取/创建更多超链接?

此外,我还想在单个<ul>中添加更多<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>
    <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>

亲切问候

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"));​

小提琴

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

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());

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

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"));
})
<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> 

这样行得通吗?