如何使用 jquery 从字符串数组生成 UL Li 列表?

发布于 2024-11-05 03:50:46 字数 661 浏览 2 评论 0原文

我有像这样的字符串数组

“美国”、“加拿大”、“阿根廷”、“亚美尼亚”、“阿鲁巴”、 '澳大利亚'、'奥地利'、'阿塞拜疆'、'巴哈马'、'孟加拉国'、 “白俄罗斯”、“比利时”**、...等

我想从字符串数组创建一个动态列表,如下所示:-

<ul class="mylist" style="z-index: 1; top: 474px; left: 228px; display: none; width: 324px;" > 
  <li class="ui-menu-item" role="menuitem">
     <a class="ui-all" tabindex="-1">
         United States
    </a>
  </li>
  <li class="ui-menu-item" role="menuitem">
     <a class="ui-all" tabindex="-1">
         Canada
    </a>
  </li>
  <li> .... </li>
   .....
</ul>

如何使用 jQuery?

I have string array like

'United States', 'Canada', 'Argentina', 'Armenia', 'Aruba',
'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bangladesh',
'Belarus', 'Belgium'**, ... etc.

I want create a dynamic list from string array like below:-

<ul class="mylist" style="z-index: 1; top: 474px; left: 228px; display: none; width: 324px;" > 
  <li class="ui-menu-item" role="menuitem">
     <a class="ui-all" tabindex="-1">
         United States
    </a>
  </li>
  <li class="ui-menu-item" role="menuitem">
     <a class="ui-all" tabindex="-1">
         Canada
    </a>
  </li>
  <li> .... </li>
   .....
</ul>

How is it possible using jQuery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

黯然 2024-11-12 03:50:49

使用数组的 join 方法的更好方法

const countries = ['United States', 'Canada', 'Argentina', 'Armenia'];
const list = '<ul class="myList"><li class="ui-menu-item" role="menuitem"><a class="ui-all" tabindex="-1">' + countries.join('</a></li><li class="ui-menu-item" role="menuitem"><a class="ui-all" tabindex="-1">') + '</a></li></ul>';

An even better approach using the array's join method

const countries = ['United States', 'Canada', 'Argentina', 'Armenia'];
const list = '<ul class="myList"><li class="ui-menu-item" role="menuitem"><a class="ui-all" tabindex="-1">' + countries.join('</a></li><li class="ui-menu-item" role="menuitem"><a class="ui-all" tabindex="-1">') + '</a></li></ul>';
怀念你的温柔 2024-11-12 03:50:49
var countries = ['United States', 'Canada', 'Argentina', 'Armenia'];
var cList = $('ul.mylist')
$.each(countries, function(i) {
    var li = $('<li/>')
        .addClass('ui-menu-item')
        .attr('role', 'menuitem')
        .appendTo(cList);
    var a = $('<a/>')
        .addClass('ui-all')
        .text( this )
        .appendTo(li);
});
var countries = ['United States', 'Canada', 'Argentina', 'Armenia'];
var cList = $('ul.mylist')
$.each(countries, function(i) {
    var li = $('<li/>')
        .addClass('ui-menu-item')
        .attr('role', 'menuitem')
        .appendTo(cList);
    var a = $('<a/>')
        .addClass('ui-all')
        .text( this )
        .appendTo(li);
});
挽袖吟 2024-11-12 03:50:49
    <script type="text/javascript" >
        function aa()
        {
            var YourArray = ['United States', 'Canada', 'Argentina', 'Armenia'];
            var ObjUl = $('<ul></ul>');
            for (i = 0; i < YourArray.length; i++)
            {
                var Objli = $('<li></li>');
                var Obja = $('<a></a>');

                ObjUl.addClass("ui-menu-item");
                ObjUl.attr("role", "menuitem");

                Obja.addClass("ui-all");
                Obja.attr("tabindex", "-1");

                Obja.text(YourArray[i]);
                Objli.append(Obja);

                ObjUl.append(Objli);
            }
            $('.DivSai').append(ObjUl);
        }
    </script>
</head>
<body onload="aa()">
    <form id="form1" runat="server">
    <div class="DivSai" >

    </div>
    </form>
</body>
    <script type="text/javascript" >
        function aa()
        {
            var YourArray = ['United States', 'Canada', 'Argentina', 'Armenia'];
            var ObjUl = $('<ul></ul>');
            for (i = 0; i < YourArray.length; i++)
            {
                var Objli = $('<li></li>');
                var Obja = $('<a></a>');

                ObjUl.addClass("ui-menu-item");
                ObjUl.attr("role", "menuitem");

                Obja.addClass("ui-all");
                Obja.attr("tabindex", "-1");

                Obja.text(YourArray[i]);
                Objli.append(Obja);

                ObjUl.append(Objli);
            }
            $('.DivSai').append(ObjUl);
        }
    </script>
</head>
<body onload="aa()">
    <form id="form1" runat="server">
    <div class="DivSai" >

    </div>
    </form>
</body>
如日中天 2024-11-12 03:50:49

Abhishek Bhalani 的其他变体:您可以使用 Array.map() 而不是 $ 。每个()

var items = ['United States', 'Canada', 'Argentina', 'Armenia'];
var cList = $('ul.mylist');
items.map( (item,i ) => {
      var li = $('<li/>')
        .addClass('ui-menu-item')
        .attr('role', 'menuitem')
        .appendTo(cList);
      $('<a class="ui-all">'+ i + ': ' + item.name + '<a/>')
        .appendTo(li);
    });

Other variation of Abhishek Bhalani: You can use Array.map() instead of $.each()

var items = ['United States', 'Canada', 'Argentina', 'Armenia'];
var cList = $('ul.mylist');
items.map( (item,i ) => {
      var li = $('<li/>')
        .addClass('ui-menu-item')
        .attr('role', 'menuitem')
        .appendTo(cList);
      $('<a class="ui-all">'+ i + ': ' + item.name + '<a/>')
        .appendTo(li);
    });
梅窗月明清似水 2024-11-12 03:50:49

没有 jQuery 也是可能的! :)

let countries = ['United States', 'Canada', 'Argentina', 'Armenia', 'Aruba'];

// The <ul> that we will add <li> elements to:
let myList = document.querySelector('ul.mylist');

// Loop over the Array of country names:
countries.forEach(function(value, index, array) {
  // Create an <li> element:
  let li = document.createElement('li');

  // Give it the desired classes & attributes:
  li.classList.add('ui-menu-item');
  li.setAttribute('role', 'menuitem');

  // Now create an <a> element:
  let a = document.createElement('a');

  // Give it the desired classes & attributes:
  a.classList.add('ui-all');
  a.tabIndex = -1;
  a.innerText = value; //  <--- the country name from our Array
  a.href = "#"

  // Now add the <a> to the <li>, and add the <li> to the <ul>
  li.appendChild(a);
  myList.appendChild(li);
});
<ul class="mylist"></ul>

It's possible without jQuery too! :)

let countries = ['United States', 'Canada', 'Argentina', 'Armenia', 'Aruba'];

// The <ul> that we will add <li> elements to:
let myList = document.querySelector('ul.mylist');

// Loop over the Array of country names:
countries.forEach(function(value, index, array) {
  // Create an <li> element:
  let li = document.createElement('li');

  // Give it the desired classes & attributes:
  li.classList.add('ui-menu-item');
  li.setAttribute('role', 'menuitem');

  // Now create an <a> element:
  let a = document.createElement('a');

  // Give it the desired classes & attributes:
  a.classList.add('ui-all');
  a.tabIndex = -1;
  a.innerText = value; //  <--- the country name from our Array
  a.href = "#"

  // Now add the <a> to the <li>, and add the <li> to the <ul>
  li.appendChild(a);
  myList.appendChild(li);
});
<ul class="mylist"></ul>

玩套路吗 2024-11-12 03:50:48
var countries = ['United States', 'Canada', 'Argentina', 'Armenia'];
var cList = $('ul.mylist')
$.each(countries, function(i)
{
    var li = $('<li/>')
        .addClass('ui-menu-item')
        .attr('role', 'menuitem')
        .appendTo(cList);
    var aaa = $('<a/>')
        .addClass('ui-all')
        .text(countries[i])
        .appendTo(li);
});
var countries = ['United States', 'Canada', 'Argentina', 'Armenia'];
var cList = $('ul.mylist')
$.each(countries, function(i)
{
    var li = $('<li/>')
        .addClass('ui-menu-item')
        .attr('role', 'menuitem')
        .appendTo(cList);
    var aaa = $('<a/>')
        .addClass('ui-all')
        .text(countries[i])
        .appendTo(li);
});
焚却相思 2024-11-12 03:50:48

使用 ES6 你可以这样写:

const countries = ['United States', 'Canada', 'Argentina', 'Armenia'];

const $ul = $('<ul>', { class: "mylist" }).append(
  countries.map(country => 
    $("<li>").append($("<a>").text(country))
  )
);

With ES6 you can write this:

const countries = ['United States', 'Canada', 'Argentina', 'Armenia'];

const $ul = $('<ul>', { class: "mylist" }).append(
  countries.map(country => 
    $("<li>").append($("<a>").text(country))
  )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文