循环遍历列表并创建<li>元素

时间:2018-03-03 22:04:15

标签: javascript jquery html5

我在html中有一个<ul>列表,如下所示:

    <ul data-role="listview" data-filter="true" data-input="#element">
            <li><a href="">
                <div class="item">
                    <h3>getting up</h3>
                    <div>
                        <h6>from bed</h6>
                        <h6>09:00</h6>
                    </div>
                </div>
            </a></li>
            <li><a href="">
                <div class="item">
                    <h3>brushing</h3>
                    <div>
                        <h6>with brush</h6>
                        <h6>09:30</h6>
                    </div>
                </div>
            </a></li>
            <li><a href="">
                <div class="item">
                    <h3>COFFEE </h3>
                    <div>
                        <h6>nescafe</h6>
                        <h6>10:30</h6>
                    </div>
                </div>
            </a></li>
            <li><a href="">
                <div class="item">
                    <h3>office</h3>
                    <div>
                        <h6>work</h6>
                        <h6>4hours</h6>
                    </div>
                </div>
            </a></li>
            <li><a href="">
                <div class="item">
                    <h3>LUNCH</h3>
                    <div>
                        <h6>canteen</h6>
                        <h6>1hour</h6>
                    </div>
                </div>
            </a></li>
            <li><a href="">
                <div class="item">
                    <h3>office</h3>
                    <div>
                        <h6>work</h6>
                        <h6>4hours</h6>
                    </div>
                </div>
            </a></li>
            <li><a href="">
                <div class="item">
                    <h3>dinner</h3>
                    <div>
                        <h6>home</h6>
                        <h6>11hours</h6>
                    </div>
                </div>
            </a></li>

        </ul>

我想做以下事情:

  1. 创建<ul>元素,附加到<li>部分
  2. 遍历列表,并将创建的<li>追加到<ul>元素? (创建数组并转换为字符串是不错的选择,但无法实现)
  3. 有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我试过这件事

var sessionsList = "<div>emptylist</div>";
var myElement = document.getElementById("#mysessioname");

var view = $(sessionsList );
view.empty();
$(noSessionsCachedMsg).appendTo(view);

var sessionsListSelector = $("#myElement").append('<ul data-role="listview" data-filter="true" data-input="#element"></ul>').find('ul');
for (var i = 0; i < 10; i++)
{
    // enter code here
    sessionsListSelector.append('<li>something</li>');
}