Как добавить объекты к незаказанному списку <ул.> с помощью jQuery

Я рекомендовал w3schools.com. Это - довольно хорошая и всесторонняя библиотека, я нахожу.

47
задан BryanH 14 October 2010 в 19:40
поделиться

3 ответа

The most efficient way is to create an array and append to the dom once.

You can make it better still by losing all the string concat from the string. Either push multiple times to the array or build the string using += and then push but it becomes a bit harder to read for some.

Also you can wrap all the items in a parent element (in this case the ul) and append that to the container for best performance. Just push the '

    ' and '
' before and after the each and append to a div.

data = [ { "userId": 1, "Username": "User_1" }, { "userId": 2, "Username": "User_2" } ]; var items = []; $.each(data, function(i, item) { items.push('
  • ' + item.Username + '
  • '); }); // close each() $('#yourUl').append(items.join(''));
     
    93
    ответ дан 26 November 2019 в 19:31
    поделиться

    Получите

      , используя синтаксис селектора jQuery, а затем вызовите append :

      $("ul#theList").append("<li><a href='url-here'>Link Text</a></li>");
      

      Подробнее см. jQuery docs информация.

    5
    ответ дан 26 November 2019 в 19:31
    поделиться
    $.each(data, function(i, item) {
    
           var li = $("<li><a></a></li>");
    
           $("#yourul").append(li);
    
           $("a",li).text(item.Username);
           $("a",li).attr("href", "http://example.com" + item.UserID);
    
        }
    
    2
    ответ дан 26 November 2019 в 19:31
    поделиться
    Другие вопросы по тегам:

    Похожие вопросы: