javascript 如何在javascript中通过for循环动态创建ul和li元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23074440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:17:41  来源:igfitidea点击:

how to dynamically create ul and li elements by for loop in javascript

javascripthtml

提问by andrewarnier

I am trying to create ul and li element in my codes by using javascript. my javascript code :

我正在尝试使用 javascript 在我的代码中创建 ul 和 li 元素。我的 javascript 代码:

 for (var i =0;i<locations.length;i++)
 {
     //how to dynamic create ul and li code as follow ul and li code?????

     }

ul and li code :

ul 和 li 代码:

<li>
        <label for="Device"><SCRIPT LANGUAGE="JavaScript">  document.write("show javascript value like the the locations[i]")  </SCRIPT></label> <input type="checkbox" id="Device" /> 
        <ol>
            <li class="file"><a href="">File 1</a></li>
            <li>
                <label for="subfolder2">Subfolder 1</label> <input type="checkbox" id="subfolder2" /> 
                <ol>
                    <li class="file"><a href="">Subfile 1</a></li>
                    <li class="file"><a href="">Subfile 2</a></li>
                    <li class="file"><a href="">Subfile 3</a></li>
                    <li class="file"><a href="">Subfile 4</a></li>
                    <li class="file"><a href="">Subfile 5</a></li>
                    <li class="file"><a href="">Subfile 6</a></li>
                </ol>
            </li>
        </ol>
    </li>

回答by Soundar

Check here...

检查here...

Script:

脚本:

var ul = document.createElement("ul");
document.body.appendChild(ul);

for (var i = 1; i <= 10; i++)
{
    var li = document.createElement("li");  
    li.className = "file";

    var a = document.createElement("a");
    a.innerHTML = "Subfile " + i;

    li.appendChild(a);
    ul.appendChild(li);
}

like this you can create your desired order..

像这样你可以创建你想要的订单..

回答by Gopal Joshi

Please check Example

请检查示例

HTML

HTML

<li>
        <label for="Device"><SCRIPT LANGUAGE="JavaScript">  document.write("show javascript value like the the locations[i]")  </SCRIPT></label> <input type="checkbox" id="Device" /> 
        <ol>
            <li class="file"><a href="">File 1</a></li>
            <li>
                <label for="subfolder2">Subfolder 1</label> <input type="checkbox" id="subfolder2" /> 
                <div id='mi'></div>
            </li>
        </ol>

    </li>

JS

JS

var text = '<ol>';
    for (var i =0;i<6;i++)
    {

             text = text + "<li class='file'><a href=''>Subfile " + i + "</a></li>";

    }
 text = text +'</ol>';

document.getElementById('mi').innerHTML=text; 

Here i have put <LI>in for loop in js and store it to variable then set it on div html.

在这里,我<LI>在 js 中放入了 for 循环并将其存储到变量中,然后将其设置在 div html 上。

回答by WASasquatch

I just answered a similar question, earlier, take a look at my example hereusing some of jQueries effects.

我刚刚回答了一个类似的问题,早些时候,看看我这里使用一些 jQueries 效果的例子

var list = $('#theList li:last-child'),
    limit = 20,
    current = 0;

function rand() {
    return Math.random().toString(36).substr(2); // Just for some random contnt
}

$('#trigger').click(function() { // We're using a static button to start the population of the list
        var end = setInterval(function() {
            if ( current == limit ) {
                current = 0;
                clearInterval(end);
            }
            list.append('<li style="display:none;color:green;">' + rand() + '</li>');
            var newList = $('#theList li:last-child');
            newList.fadeIn();
            var colorEnd = setInterval(function() {
                    newList.css('color', 'black');
                    clearInterval(colorEnd);
            }, 350);
            current =  current + 1;
        }, 300);
});