Is there a jQuery equivalent of the MooTools Element () constructor?

I am going to run a javascript engine for my site and have started some prototypes using MooTools. I really like being able to do things like this:

function showLeagues(leagues) {
    var leagueList = $("leagues");
    leagueList.empty();
    for(var i = 0; i<leagues.length; ++i) {
        var listItem = getLeagueListElement(leagues[i]);
        leagueList.adopt(listItem);
    }
}

function getLeagueListElement(league) {
    var listItem = new Element('li');
    var newElement = new Element('a', {
        'html': league.name,
        'href': '?league='+league.key,
        'events': {
                'click': function() { showLeague(league); return false; }
        }
    });
    listItem.adopt(newElement);
    return listItem;
}

From what I saw, jQuery's take methods only use html strings or DOM elements. Is there any jQuery equivalent to the MooTools Element ?


EDIT: The big thing I'm looking for here is to programmatically bind my click event to a link.
+3
source share
2 answers

syntactically, it might be better to use jQuery for this, but it is probably more efficient to use

  document.createElement('li')

.

flydom , dom-. ( , )


. jQuery ( "<html> </html> " ) , ():

jQuery(matcher) --> function(matcher)
{
   return jQuery.fn.init(matcher) --> function(matcher)
   {
      return  this.setArray(
        jQuery.makeArray(
           jQuery.clean(matcher) --> function(matcher)
           { 
               div = document.createElement('div');
               div.innerHTML = matcher;
               return div.childNodes;
           }
        )
      );
   }
}

, , "document.createElement" , , "", , (.. $( datahere )), document.createElement imho , , .

: jQuery(document.createElement('div')) , ():

jQuery(matcher) --> function(matcher)
{
   return jQuery.fn.init(matcher) --> function(matcher)
   {
       this[0] = matcher; 
       this.length = 1; 
       return this; 
   }
}
+5

jQuery. HTML-, .

function showLeagues(leagues) {
    var $leagueList = $("#leagues");
    $leagueList.empty();
    $.each(leagues, function (index, league) {
        $leagueList.append(getLeagueListElement(league));
    });
}

function getLeagueListElement(league) {
    return $('<li></li>')
        .append($('<a></a>')
            .html(league.name)
            .attr('href', '?league=' + league.key)
            .click(function() {
                showLeague(league);
                return false;
            })
        )
    ;
}
+3

Source: https://habr.com/ru/post/1697956/


All Articles