【问题标题】:Is there a way to create an unordered html list from an array in Javascript?有没有办法从Javascript中的数组创建一个无序的html列表?
【发布时间】:2019-08-26 19:20:42
【问题描述】:

我想使用纯 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

【问题讨论】:

  • 您需要向我们提供更多信息。你想创建一个无序的什么?问题到底出在哪里,问题是什么?请更明确。
  • lol 标题听起来像是您想在将数组传递给 html 之前主动打乱数组:d

标签: javascript html ruby haml


【解决方案1】:
<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>

【讨论】:

    猜你喜欢
    • 2020-06-21
    • 2021-08-30
    • 1970-01-01
    • 2021-01-28
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多