如何使用javascript创建有序列表?

如何使用javascript创建有序列表?,javascript,list,numbered,Javascript,List,Numbered,假设我有这个 <ol> <li>V1 One</li> <li>V1 Two</li> <li>V1 Three</li> <li>V1 Four</li> </ol>` 其中i++将从1开始每次增加 是的,我知道李先生提供了订购单 您可以有一个空div,并使用javascript在div中输入innerHTML function listdown(){

假设我有这个

<ol>
  <li>V1 One</li>
  <li>V1 Two</li>
  <li>V1 Three</li>
  <li>V1 Four</li>
</ol>`
其中i++将从1开始每次增加


是的,我知道李先生提供了订购单

您可以有一个空div,并使用javascript在div中输入innerHTML

function listdown(){
  var startlist = "<ol>";
  var endlist = "</ol>";
  *use a for loop to enter the <LI>, this way, you can do much more with each item*
  document.getElementById("emptydiv").innerHTML = startlist + LI list + endlist;
}
函数listdown(){
var-startist=“”;
var endlist=“”;
*使用for循环输入
  • ,这样,您可以对每个项目执行更多操作* document.getElementById(“emptydiv”).innerHTML=startlist+LI list+endlist; }
  • 编辑


    尽管如此,JQuery仍然是最好的选择

    您可以拥有一个空div,并使用javascript在div中输入innerHTML

    function listdown(){
      var startlist = "<ol>";
      var endlist = "</ol>";
      *use a for loop to enter the <LI>, this way, you can do much more with each item*
      document.getElementById("emptydiv").innerHTML = startlist + LI list + endlist;
    }
    
    函数listdown(){
    var-startist=“”;
    var endlist=“”;
    *使用for循环输入
  • ,这样,您可以对每个项目执行更多操作* document.getElementById(“emptydiv”).innerHTML=startlist+LI list+endlist; }
  • 编辑


    尽管如此,JQuery仍然是最好的选择

    我建议您学习(使用)javascript框架来实现这一点。它将快速加速javascript开发。Jquery目前非常流行,我认为您也应该使用它。Stackoverflow allready有一个主题,使用Jquery描述您想要什么=>

    我建议您学习(使用)javascript框架来实现这一点。它将快速加速javascript开发。Jquery目前非常流行,我认为您也应该使用它。Stackoverflow allready有一个使用Jquery描述您想要什么的主题=>

    也许您想循环通过
    的子对象并操作它们的
    内部HTML

    // Get the <ol> object from the DOM
    var chillun = document.getElementById("yourOL").childNodes;
    
    // Loop through the children of the <ol>
    for(i=0;i<chillun.length;i++) {
      // Test that the node is indeed an <li>
      if(chillun[i].nodeName=='LI') {
        // Change this line to manipulate the text however you need
        chillun[i].innerHTML = i;
      }
    }
    
    //从DOM获取对象
    var chillun=document.getElementById(“yourOL”).childNodes;
    //循环遍历
    
    对于(i=0;i也许您希望遍历
    的子对象并操作它们的
    innerHTML

    // Get the <ol> object from the DOM
    var chillun = document.getElementById("yourOL").childNodes;
    
    // Loop through the children of the <ol>
    for(i=0;i<chillun.length;i++) {
      // Test that the node is indeed an <li>
      if(chillun[i].nodeName=='LI') {
        // Change this line to manipulate the text however you need
        chillun[i].innerHTML = i;
      }
    }
    
    //从DOM获取对象
    var chillun=document.getElementById(“yourOL”).childNodes;
    //循环遍历
    
    对于(i=0;i你可以这样做:

    let ol = document.querySelector('ol');
    let i = 1;
    ol.querySelectorAll('li').forEach(function(li) {
        li.textContent = (i++) + ' ' + li.textContent;
    });
    

    你可以这样做:

    let ol = document.querySelector('ol');
    let i = 1;
    ol.querySelectorAll('li').forEach(function(li) {
        li.textContent = (i++) + ' ' + li.textContent;
    });
    

    换句话说,你想要操作的数据从哪里来?换句话说,你想要操作的数据从哪里来?另外,jQuery模板插件在防止HTML进入JavaScript方面非常出色。另外,jQuery模板插件在防止HTML进入JavaScript方面也非常出色。