繁体   English   中英

有没有办法从Javascript中的数组创建无序的html列表?

[英]Is there a way to create an unordered html list from an array in Javascript?

我想使用纯 html 和 javascript 而不是 HAML。 在我使用这个表单项目之前,我像这样循环遍历数组

- @item1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
- item1.each_with_index do |item, i|
   %li.option
      %input.option-input{name: 'options', type: 'radio', value: i, id: "options-#{i}"}/
      %label.option-label{:for => "options-#{i}"}= item1
<script>
    (function(){
        const items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
        const ul = document.createElement('UL');
        for (let i=0; i< items.length; i++){
            const item = items[i];
            const li = document.createElement('LI');
            const label = document.createElement('LABEL');
            const input = document.createElement('INPUT');
            input.value = item;
            input.setAttribute('id', 'options-' + i);
            input.setAttribute('type', 'radio');
            input.setAttribute('name', 'options');
            label.appendChild(input);
            label.setAttribute('for', 'options-' + i);
            label.appendChild(document.createTextNode(item));
            li.appendChild(label);
            ul.appendChild(li);
        }
        document.body.appendChild(ul);
    })();
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM