如何在有序列表中打印出列表元素的索引?

时间:2019-04-18 14:18:09

标签: javascript dynamic

我有这段代码,单击按钮即可在列表内生成一组元素。每个组都以1,2,3等升序排列在有序列表中。我需要将这些数字存储起来以便在数据库中使用。 目前,我还无法找到一种方法来标识列表中每个组的索引号,并将其添加为属性。 这是我用于生成组的JS:

function spawnSilly() //spawn chapters function
        {
            var div = document.createElement("LI");
            var input = document.createElement("INPUT");
            var button = document.createElement("BUTTON");
            var del_button = document.createElement("BUTTON")
            input.setAttribute("type", "text");
            input.setAttribute("placeholder", "Title");
            input.setAttribute("id", "var timestamp = new Date().getUTCMilliseconds();")
            button.setAttribute("type", "button");
            button.setAttribute("onClick", "redirect()");
            button.setAttribute("id", "var timestamp = new Date().getUTCMilliseconds();")
            button.innerHTML = "Edit";
            div.setAttribute("id", "var timestamp = new Date().getUTCMilliseconds();")
            del_button.setAttribute("id", "var timestamp = new Date().getUTCMilliseconds();")
            del_button.innerHTML = "Delete";
            del_button.setAttribute("onClick", "removeElement(this.id)")
            div.appendChild(input)
            div.appendChild(button)
            div.appendChild(del_button);

            var list = document.getElementById("spawnList");
            list.insertBefore(div, list.childNodes[0]);
            set_index();
        }

我一直在使用此JS代码添加索引,但是由于无法打印出已生成的列表元素的索引,因此我无法确定它是否正常工作。

function set_index()
        {
            var ol = document.getElementById('spawnList');

            // select the list items
            var lists = ol.getElementsByTagName('li');

            var l = lists.length; // total items
            //custom list id's via loop
            for (var i=1;i<=l;i++)
            {
            list[i].index = i;
            }

        }

这是我的HTML:

<ol id="spawnList">


    </ol>
    <button id="spawnbtn" onClick="spawnSilly(); ">Add</button>

这真是困扰我,任何帮助都太棒了!谢谢:)

1 个答案:

答案 0 :(得分:0)

我改变了在ol标签中添加li标签并显示索引的方式。

var list = document.getElementById("spawnList");         

  list.appendChild(div);

var index = 1;
function spawnSilly() //spawn chapters function
        {
            var div = document.createElement("LI");
            var input = document.createElement("INPUT");
            var button = document.createElement("BUTTON");
            var del_button = document.createElement("BUTTON")
            input.setAttribute("type", "text");
            input.setAttribute("placeholder", "Title");
            input.setAttribute("id", "var timestamp = new Date().getUTCMilliseconds();")
            button.setAttribute("type", "button");
            button.setAttribute("onClick", "redirect()");
            button.setAttribute("id", "var timestamp = new Date().getUTCMilliseconds();")
            button.innerHTML = "Edit";
            div.setAttribute("id", "var timestamp = new Date().getUTCMilliseconds();")
            del_button.setAttribute("id", "var timestamp = new Date().getUTCMilliseconds();")
            del_button.innerHTML = "Delete";
            del_button.setAttribute("onClick", "removeElement(this.id)")
            div.appendChild(input)
            div.appendChild(button)
            div.appendChild(del_button);

            var list = document.getElementById("spawnList");
            
            //var li = document.createElement("li");
            
  
  list.appendChild(div);
            console.log(index++);
            //list.insertBefore(div, list.childNodes[0]);
            //set_index();
        }

function set_index()
        {
            var ol = document.getElementById('spawnList');

            // select the list items
            var lists = ol.getElementsByTagName('li');
            if(lists != undefined){
              var l = lists.length; // total items
            //custom list id's via loop
            for (var i=1;i<=l;i++)
            {
            list[i].index = i;
            }
            }

          

        }
<ol id="spawnList">


    </ol>
    <button id="spawnbtn" onClick="spawnSilly(); ">Add</button>