const wait = time => new Promise(r => setTimeout(r, time));wait(1000)
.then(() => { console.log(1); return wait(1000); })
.then(() => { console.log(2); return wait(1000); })
.then(() => { console.log(3); });[ 1, 2, 3 ].reduce((promise, n) => promise
.then(() => wait(1000))
.then(() => console.log(n))
, Promise.resolve());
// или
(async function(...args) {
for (const n of args) {
await wait(1000);
console.log(n);
}
})(1, 2, 3);
const itemSelector = '.parent';
const className = 'xxx';
document.querySelectorAll(itemSelector).forEach(n => {
n.addEventListener('mouseenter', onHover);
n.addEventListener('mouseleave', onHover);
});
function onHover(e) {
const state = e.type === 'mouseenter';
for (
let el = this;
(el = el.nextElementSibling) && !el.matches(itemSelector);
el.classList.toggle(className, state)
) ;
}.parent, чтобы создать видимость, будто бы стили не применялись:.parent:hover ~ .child {
...
}
.parent:hover ~ .parent ~ .child {
...
}
document.querySelectorAll('.selectit > input').forEach(n => {
n.checked = genres.includes(n.parentNode.innerText.trim());
});for (const n of document.getElementsByClassName('selectit')) {
n.children[0].checked = genres.indexOf(n.textContent.trim()) > -1;
}
ind.map(((data, n) => data[n]).bind(null, arr.reduce((acc, row) => {
const d = acc[row[0]];
row.forEach((n, i) => i && (d[i] = d[i] || []).push(n));
return acc;
}, ind.reduce((acc, n) => (acc[n] = [ n ], acc), {}))))
array.reduce((acc, el) => {
el.forEach((n, i) => {
acc[i] = acc[i] || [];
n.forEach((m, j) => (acc[i][j] = acc[i][j] || []).push(...[].concat(m)));
});
return acc;
}, [])
const result = ind.reduce((acc, col) => {
arr.forEach((n, i) => {
acc[i].push(i ? n.filter((m, j) => arr[0][j] === col) : col);
});
return acc;
}, arr.map(() => []));const indObj = Object.fromEntries(ind.map((n, i) => [ n, i ]));
const result = arr.reduce((acc, n, i) => (
i && n.forEach((m, j) => acc[i][indObj[arr[0][j]]].push(m)),
acc
), arr.map((_, i) => ind.map(n => i ? [] : n)));
const min = 666;
const inputSelector = 'input';
const buttonSelector = 'button';const input = document.querySelector(inputSelector);
const button = document.querySelector(buttonSelector);
input.addEventListener('input', e => button.disabled = e.target.value < min);
input.dispatchEvent(new Event('input'));const $input = $(inputSelector);
const $button = $(buttonSelector);
$input.on('input', () => $button.prop('disabled', $input.val() < min)).trigger('input');
attr - это метод jquery.e.attr('href', 'tel:' + tel); на $(e).attr('href', 'tel:' + tel);.ready уносите скрипт в самый низ body или слушаете событие DOMContentLoaded. Ну а сама установка значения атрибуту href может выглядеть следующим образом:document.querySelectorAll('.phone-number').forEach(n => {
n.href = `tel:${n.text.replace(/\s/g, '')}`;
});
// или
for (const n of document.getElementsByClassName('phone-number')) {
n.setAttribute('href', 'tel:'.concat(...n.textContent.match(/\S/g) || []));
}forEach:$('.phone-number').attr('href', function() {
return 'tel:' + $(this).text().split(/\s/).join('');
});forEach (что указывает на наличие нескольких элементов) сочетается с показанным в разметке id="phone_number"? Вы что, не слышали, что значения id должны быть уникальны? Уберите id. Или, если элемент всё-таки один, не надо использовать методы перебора, чтобы не вводить в заблуждение людей, которые могут увидеть ваш код (себя в том числе, если вдруг вернётесь к коду спустя достаточно продолжительный отрезок времени, чтобы забыть, что там у вас и как).
const className = 'qq-upload-file';.const elements = document.querySelectorAll(`.${className}`);
// или
const elements = document.getElementsByClassName(className);const getText = el => el.textContent;
// или
const getText = el => el.innerText;
// или (т.к., вложенных элементов нет)
const getText = el => el.innerHTML;const texts = Array.from(elements, getText);
// или
const texts = Array.prototype.map.call(elements, getText);
// или
const texts = [];
for (const n of elements) {
texts.push(getText(n));
}
// или
const texts = [];
for (let i = 0; i < elements.length; i++) {
texts[i] = getText(elements[i]);
}
// или
const texts = (function get(i, n = elements.item(i)) {
return n ? [ getText(n), ...get(i + 1) ] : [];
})(0);const uniqueCount = new Set(texts).size;
// или
const { size: uniqueCount } = new Map(texts.map(n => [ n, n ]));
// или
const [ uniqueCount ] = texts.reduce((acc, n) => {
acc[0] += !(acc[1][n] = acc[1].hasOwnProperty(n));
return acc;
}, [ 0, {} ]);
// или
const uniqueCount = texts.filter((n, i, a) => i === a.indexOf(n)).length;
const inputs = document.querySelectorAll('form input');
// или
const inputs = document.forms[0].elements;
// или
const inputs = document.querySelector('form').getElementsByTagName('input');const getName = el => el.name;
// или
const getName = el => el.getAttribute('name');
// или
const getName = el => el.attributes.name.value;const names = Array.from(inputs, getName);
// или
const names = Array.prototype.map.call(inputs, getName);
// или
const names = [];
for (const n of inputs) {
names.push(getName(n));
}
// или
const names = [];
for (let i = 0; i < inputs.length; i++) {
names[i] = getName(inputs[i]);
}
// или
const names = (function get(i, n = inputs.item(i)) {
return n ? [ getName(n), ...get(i + 1) ] : [];
})(0);
const result = Object
.entries(arr.reduce((acc, n, i) => ((acc[n] = acc[n] || []).push(i), acc), {}))
.map(n => n[1].length > 1 ? { label: n[0], indexes: n[1] } : n[0]);const result = Array.from(
arr.reduce((acc, n, i) => (acc.set(n, acc.get(n) || []).get(n).push(i), acc), new Map),
([ label, indexes ]) => ~-indexes.length ? { label, indexes } : label
);
const result = Object.values(arr.reduce((acc, n) => (
(acc[n.ID] = acc[n.ID] || { ID: n.ID })[n.FIELD] = n.VALUE,
acc
), {}));
setInterval(count => {
const
len = count.length,
val = `${+count.map(n => n.textContent).join('') + 1}`.padStart(len, 0).slice(-len);
count.forEach((n, i) => n.textContent = val[i]);
}, 50, [...document.querySelectorAll('.count')]);
document.querySelectorAll('div[name] ul[name]').forEach(n => {
if (n.getAttribute('name') !== n.closest('div[name]').getAttribute('name')) {
n.remove();
}
});for (const n of document.querySelectorAll('div[name]')) {
for (const m of n.querySelectorAll(`ul:not([name="${n.attributes.name.value}"])`)) {
m.parentNode.removeChild(m);
}
}