Usually I add a page code received through js in this way. But maybe there is some more convenient method?
for (var i = 0; i < n; i++) { if ($data.data.MOW[i]) { $html.append( '<p>Пользователь: <span class="user">' + user.nickname + '</p>') } }
Usually I add a page code received through js in this way. But maybe there is some more convenient method?
for (var i = 0; i < n; i++) { if ($data.data.MOW[i]) { $html.append( '<p>Пользователь: <span class="user">' + user.nickname + '</p>') } }
It depends on what is “convenient” for you.
Optimization
Manipulations with DOM always take a lot of time, so you need to minimize the number of calls to the DOM, given that you do it in a loop.
var html = []; for (var i = 0; i < n; i++) { if ($data.data.MOW[i]) { html.push('<p>Пользователь: <span class="user">' + user.nickname + '</p>'); } } $html.append(html.join('\n'));
Pattern Lines (ES6)
In the new ECMAScript 6 specification, ' pattern strings ' have appeared.
for (var i = 0; i < n; i++) { if ($data.data.MOW[i]) { $html.append( `<p>Пользователь: <span class="user">${user.nickname}</p>`) } }
Of course, you can combine both methods above.
Template engine
All (or almost) template engines have the ability to work with loops to draw elements. From template engines, I would recommend doT.js ( doT.js + jQuery article). But not one doT.js are one, there are still many different template engines from which you can choose to your taste here .
Source: https://ru.stackoverflow.com/questions/551651/
All Articles