const elems = [...document.querySelectorAll('.class1')];
const index = elems.findIndex(n => n.classList.contains('class2'));
const elems = document.querySelectorAll('.class1');
let index = elems.length;
while (--index >= 0 && !elems[index].matches('.class2')) ;
const index = Array.prototype.indexOf.call(
document.querySelectorAll('.class1'),
document.querySelector('.class2')
);
<div class="country">
<input class="country-code">
<img class="country-flag">
</div>
function updateFlag(el) {
const country = countries.find(n => !el.value.indexOf(n.code));
el.closest('.country').querySelector('.country-flag').src = country ? country.flag : '';
}
document.addEventListener('input', function(e) {
if (e.target.classList.contains('country-code')) {
updateFlag(e.target);
}
});
document.querySelectorAll('.country-code').forEach(updateFlag);
const countries = [
{ code: ..., flag: ... },
{ code: ..., flag: ... },
...
];
const country = countries.find(n => !inputCode.value.indexOf(n.code));
inputFlag.src = country ? country.flag : '';
<input type="number" min="0" max="100">
<table>
<tbody></tbody>
</table>
const input = document.querySelector('input');
const table = document.querySelector('table');
input.addEventListener('input', ({ target: t }) => {
const count = t.value = Math.max(0, t.value | 0);
const [ tbody ] = table.tBodies;
const { rows } = tbody;
for (; rows.length > count; tbody.deleteRow(-1)) ;
while (rows.length < count) {
const row = tbody.insertRow();
row.insertCell().textContent = '#' + rows.length;
row.insertCell().textContent = Math.random() * 1e3 | 0;
}
});
// или
input.oninput = function() {
const count = this.value = Math.max(0, +this.value || 0);
const tbody = table.querySelector('tbody');
const rows = tbody.children;
Array.prototype.slice.call(rows, Math.min(rows.length, count)).forEach(n => n.remove());
tbody.insertAdjacentHTML('beforeend', Array
.from({ length: Math.max(0, count - rows.length) }, (n, i) => `
<tr>
<td>#${rows.length + i + 1}</td>
<td>${Math.floor(Math.random() * 1000)}</td>
</tr>`)
.join('')
);
};
document.addEventListener('click', function(e) {
if (!e.target.closest('.tileset-showitems')) {
tilesetShowListRemove();
}
});
- .active {
+ .active .box {
const containerSelector = '.tileset-showitems';
const buttonSelector = `${containerSelector} .tileset-showitems-trigger`;
const activeClass = 'active';
const closeAllExcept = container => document
.querySelectorAll(`${containerSelector}.${activeClass}`)
.forEach(n => n !== container && n.classList.remove(activeClass));
document.addEventListener('click', ({ target: t }) => {
const button = t.closest(buttonSelector);
const container = t.closest(containerSelector);
if (button) {
container.classList.toggle(activeClass);
}
closeAllExcept(container);
});
window.addEventListener('keydown', e => e.key === 'Escape' && closeAllExcept());
for (const n of el.options) {
for (const m of elValue) {
if (n.value === m) {
n.selected = true;
break;
}
}
}
Array.prototype.forEach.call(
el,
n => n.selected = elValue.includes(n.value)
);
el
.querySelectorAll(elValue.map(n => `[value="${n}"]`))
.forEach(n => n.selected = true);
а где в верстке у тебя эти точки указаны?
const points = [
{ max: 200, text: 'hello, world!!' },
{ max: 800, text: 'fuck the world' },
{ max: Infinity, text: 'fuck everything' },
];
window.addEventListener('scroll', function() {
const
scroll = document.documentElement.scrollTop,
{ text } = points.find(n => n.max >= scroll);
document.querySelector('.fixed').textContent = text;
});
const newArr = arr.map(function({ ...n }) {
return this.reduce((acc, k) => (acc[k] = acc[k].toLowerCase(), acc), n);
}, [ 'firstName', 'lastName' ]);
const keys = [ 'firstName', 'lastName' ];
arr.forEach(n => keys.forEach(k => n[k] = n[k].toLowerCase()));
const data = $('table tr')
.get()
.map(tr => $('input', tr).get().map(n => $(n).val()))
.filter(row => row[row.length - 1])
.map(row => `(${row.map(n => `'${n}'`).join(', ')})`);
const data = Array.prototype.reduce.call(
document.querySelector('table').rows,
(acc, { cells, lastElementChild: last }) => (
last.lastElementChild.value && acc.push(`(${Array
.from(cells, n => `'${n.lastElementChild.value}'`)
.join(', ')})`
),
acc
),
[]
);
Как поправить код?
const discounts = [
{ percent: 0, sum: 0 },
{ percent: 2, sum: 4000 },
{ percent: 3, sum: 10000 },
{ percent: 4, sum: 20000 },
{ percent: 5, sum: 50000 },
];
const discount = discounts.reduce((p, c) => c.sum <= pricechange ? c : p);
const discountedPrice = pricechange * (100 - discount.percent) / 100;
const discounts = [
{ percent: 0, sum: 4000 },
{ percent: 2, sum: 10000 },
{ percent: 3, sum: 20000 },
{ percent: 4, sum: 50000 },
{ percent: 5, sum: Infinity },
];
const discount = discounts.find(n => n.sum > pricechange);
const buttons = document.querySelectorAll('.item1');
const targets = document.querySelectorAll('.item2');
const className = 'red';
const updateClass = action => ({ target: t }) => {
const index = Array.prototype.indexOf.call(buttons, t);
if (index !== -1) {
targets[index].classList[action](className);
}
};
const wrapper = document.querySelector('#wrap1');
wrapper.addEventListener('click', updateClass('add'));
wrapper.addEventListener('dblclick', updateClass('remove'));
let state = true;
document.querySelector('button').addEventListener('click', e => {
state = !state;
document.querySelectorAll('circle').forEach((n, i, a) => {
setTimeout(() => {
n.classList.toggle('st0', state);
e.target.disabled = (state ? a.length - i : i + 1) !== a.length;
}, (state ? a.length - i : i) * 3);
});
});
let state = true;
const button = document.querySelector('button');
const circles = document.querySelectorAll('circle');
button.addEventListener('click', () => {
state = !state;
toggleCircle(0);
});
function toggleCircle(i) {
if (i < circles.length) {
setTimeout(() => {
const index = state ? circles.length - i - 1 : i;
circles[index].classList.toggle('st0', state);
button.disabled = i !== circles.length - 1;
toggleCircle(i + 1);
}, 3);
}
}
let state = true;
const circles = document.querySelectorAll('circle');
document.querySelector('button').addEventListener('click', function() {
this.disabled = true;
state = !state;
let i = 0;
const intervalID = setInterval(() => {
const index = state ? circles.length - i - 1 : i;
circles[index].classList.toggle('st0', state);
if (++i === circles.length) {
this.disabled = false;
clearInterval(intervalID);
}
}, 3);
});
moment.utc(moment.duration(endTime) - moment.duration(startTime)).format('HH:mm')
var delayAnim = 4; <...> 'animation-delay': '.' + delayAnim + 's'
'animation-delay': (delayAnim + 0.2 * index) + 's'
удаляются последовательно только теги tr <...> вложенные теги td остаются
table.appendChild(tr); <...> table.appendChild(tdLeft); // "Название" table.appendChild(tdRight); //"Описание" table.appendChild(tdDelete); //"Навигация"
function getWinCount(team) {
const el = Array
.from(document.querySelectorAll('.table-item__name'))
.find(n => n.textContent === team);
return el ? +el.closest('tr').children[3].textContent : null;
}
const wins = getWinCount('Уфа');
Мой браузер не поддерживает ES-2015
function getWinCount(team) {
var el = null;
[].concat.apply([], document.querySelectorAll('.table-item__name')).forEach(function(n) {
if (!el && n.textContent === team) {
el = n;
}
});
if (!el) {
return null;
}
do {
el = el.parentNode;
} while (el.tagName !== 'TR');
return +el.children[3].textContent;
}