开发者

jQuery html injection from list without wrapping

开发者 https://www.devze.com 2023-04-03 02:38 出处:网络
I\'m following quide in http://api.jquery.com/jQuery.getJSON/ and everything is great e开发者_运维百科xcept I\'m puzzled about injection part.

I'm following quide in http://api.jquery.com/jQuery.getJSON/ and everything is great e开发者_运维百科xcept I'm puzzled about injection part.

It's hard for me to understand what this exactly does:

$('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
}).appendTo('body');

Well appendTo() is clear. But how it makes html out of JS array and how to remove wrapping? Can this be translated to group of simple commands?


That could be rewritten like so:

 var element = $('<ul/>');
 element.addClass('my-new-list').html(items.join(''));
 element.appendTo('body');

It's just creating a new element with the specified values and properties.

From this link, it is the jQuery( html,props ) overload.


This creates new <ul>, with class my-new-list and joins items variable. After that, it is all appended to body. It could be like

var ul = $('<ul/>')
ul.addClass('my-new-list');
ul.html(items.join(''));
ul.appendTo(body);


var elem = $('<ul/>');          //var elem = document.createElement("ul");
elem.addClass('my-new-list');   //elem.className = 'my-new-list';
elem.html(items.join(''));      //elem.innerHTML = items.join('');
elem.appendTo('body');          //document.body.append(elm);
0

精彩评论

暂无评论...
验证码 换一张
取 消