оборачивается не полностью
childNodes
представляет собой динамическую коллекцию, т.е., при добавлении или удалении узлов она обновляется без каких-либо действий с вашей стороны. Поэтому, когда вы добавляете в wrapper
нулевой узел, он тут же пропадает из item.childNodes
, а у оставшихся узлов позиция уменьшается на единицу - тот, что был первым, становится нулевым, второй первым и так далее. Так что когда for...of
переходит к следующему узлу, им оказывается не тот, что изначально имел индекс 1
, а расположенный за ним. Бывший первый, а теперь нулевой, оказывается пропущен. Аналогичным образом будут пропущены и все последующие узлы, изначально имевшие нечётные индексы.for (let n; n = item.firstChild;) {
wrapper.appendChild(n);
}
childNodes
от конца к началу:for (let i = item.childNodes.length; i--;) {
wrapper.prepend(item.childNodes[i]);
}
childNodes
, а массив:for (const n of [...item.childNodes]) {
wrapper.insertBefore(n, null);
}
append
может принимать несколько параметров, так что переносим сразу всё:document.querySelectorAll('.www').forEach(n => {
const wrapper = document.createElement('div');
wrapper.classList.add('red');
wrapper.append(...n.childNodes);
n.append(wrapper);
});
for (const n of document.getElementsByClassName('www')) {
n.innerHTML = `<div class="red">${n.innerHTML}</div>`;
}
computed: {
summary() {
return (this.choosenSize?.cost ?? 0) + this.choosenTopping.reduce((acc, n) => acc + n.cost, 0);
},
},
from random import choice
chars = '0123456789abcdefghijklmnopqrstuvwxyz'
section_size = 4
sections_num = 5
string = '-'.join(''.join(choice(chars) for j in range(section_size)) for i in range(sections_num))
const sorted = arr
.map(n => [
n,
+new URLSearchParams(n.querySelector('a').href.split('?').pop()).get('value') || Infinity,
])
.sort((a, b) => a[1] - b[1])
.map(n => n[0]);
parentEl.append(...Array
.from(parentEl.querySelectorAll('a'), n => [
n.parentNode,
Number(n.getAttribute('href').match(/(?<=value=)\d+/)) || Infinity,
])
.sort((a, b) => a[1] - b[1])
.map(n => n[0])
);
const Figure = ({ type, ...props }) => <div className={type} {...props}></div>;
class App extends React.Component {
state = {
types: [ 'circle', 'square', 'triangle' ],
figures: [],
}
add(type) {
this.setState(({ figures }) => ({
figures: [ ...figures, type ],
}));
}
render() {
const { types, figures } = this.state;
return (
<div>
<div>
{types.map(n => <Figure type={n} onClick={() => this.add(n)} />)}
</div>
<div>
{figures.map(n => <Figure type={n} />)}
</div>
</div>
);
}
}
function Preloader({ Tag = 'h1', children }) {
return (
<Tag className={s.wrapper}>
<div className={s.preloader}></div>
{children}
</Tag>
);
}
<div className="App">
<Preloader>hello, world!!</Preloader>
<Preloader Tag="h2">fuck the world</Preloader>
</div>
const updateValue = input =>
input.previousElementSibling.querySelector('.p_value').innerText = input.value;
class="xxx"
.document.addEventListener('input', ({ target: t }) => {
if (t.classList.contains('xxx')) {
updateValue(t);
}
});
document.querySelectorAll('.xxx').forEach(function(n) {
n.addEventListener('input', this);
}, e => updateValue(e.target));
watch: { cloneItems(old, cur) { this.prevItems = old; },
computed: { cloneItems: () => this.items.slice(),
prev items: <div v-for="item in items" :key="item">
this.items = [...this.items, Date.now()];
const cols = document.querySelectorAll('.col');
cols.forEach(n => {
n.addEventListener('mouseover', onHover);
n.addEventListener('mouseout', onHover);
});
function onHover(e) {
const index = [...this.children].findIndex(n => n.contains(e.target));
if (index !== -1) {
const t = e.type === 'mouseover';
cols.forEach(n => n.children[index].classList.toggle('hovered', t));
}
}
const links = document.querySelectorAll('.nav-about a');
links.forEach((n, i) => n.attributes.href.value += i + 1);
// или
for (const [ i, n ] of links.entries()) {
n.setAttribute('href', n.getAttribute('href').concat(-~i));
}
// или
for (let i = 0; i < links.length;) {
links[i].href = links[i].href.replace(/.*(#.*)/, `$1${++i}`);
}
$('button').click(() => $('select').prop('selectedIndex', 0));
document.querySelector('button').addEventListener('click', () => {
document.querySelectorAll('select').forEach(n => {
// Какие тут есть варианты:
// 1. Установить индекс выбранного option'а
n.selectedIndex = 0;
// 2. Установить select'у значение option'а, который должен быть выбран
// (чтобы заработало, надо будет добавить value="" option'ам)
n.value = '';
// 3. Назначить true свойству selected того option'а, на который надо переключиться
n[0].selected = true;
// или
// n.options[0].selected = true;
// n.children[0].selected = true;
// n.firstElementChild.selected = true;
// n.querySelector('option').selected = true;
});
});
const arrs = [ arr1, arr2 ];
), дальше есть варианты:const result = arrs[0].map((_, i) => arrs.flatMap(arr => arr[i]));
const result = arrs.reduce((acc, arr) => (
arr.forEach((n, i) => (acc[i] ??= []).push(...n)),
acc
), []);
const result = [];
for (const arr of arrs) {
for (const [ i, n ] of arr.entries()) {
if (!result[i]) {
result[i] = [];
}
for (const m of n) {
result[i][result[i].length] = m;
}
}
}
function* zip(data, defaultValue = null) {
const iterators = Array.from(data, n => n[Symbol.iterator]());
for (let doneAll = false; doneAll = !doneAll;) {
const values = [];
for (const n of iterators) {
const { value, done } = n.next();
values.push(done ? defaultValue : value);
doneAll &&= done;
}
if (!doneAll) {
yield values;
}
}
}
const result = Array.from(zip(arrs), n => n.flat());
watch: {
'product.qty'(val) {
this.product.qty = Math.max(1, Math.min(99, val | 0));
},
},
v-for
и это элемент массива, то можно установить глубокое наблюдение за всем массивом:watch: {
products: {
deep: true,
handler: val => val.forEach(n => n.qty = Math.max(1, Math.min(99, n.qty | 0))),
},
},
methods: {
onInput(e) {
if (e.isTrusted) {
e.target.value = Math.max(1, Math.min(99, e.target.value | 0));
e.target.dispatchEvent(new Event('input'));
}
},
},
<input
@input="onInput"
...
function onInput({ isTrusted, target: t }) {
if (isTrusted) {
t.value = Math.max(t.min, Math.min(t.max, t.value | 0));
t.dispatchEvent(new Event('input'));
}
}
const minmaxDirective = {
mounted: el => el.addEventListener('input', onInput),
unbind: el => el.removeEventListener('input', onInput),
};
app.directive('minmax', minmaxDirective);
<input
v-minmax
min="1"
max="99"
...
box.querySelectorAll('.item').forEach((n, i) => n.dataset.priority = -~i);
// или
for (const [ i, n ] of box.querySelectorAll('.item').entries()) {
n.setAttribute('data-priority', i + 1);
}
// или
for (let i = box.children.length; i;) {
const attr = document.createAttribute('data-priority');
attr.value = i;
box.children[--i].attributes.setNamedItem(attr);
}
// или
(function next(i, n) {
n && next(n.attributes['data-priority'].value = ++i, n.nextElementSibling);
})(0, box.firstElementChild);
e.get('target').properties.get('имяСвойстваСодержащегоId')
shop.id
)?<div data-id="{{ properties.имяСвойстваСодержащегоId }}">