const cards = Array.from(
document.querySelectorAll('[data-component="card"]'),
n => new Card(n)
);
constructor(el) {
if (typeof el === 'string') {
return Array.from(document.querySelectorAll(el), n => new Card(n));
}
// дальше всё по-старому
const cards = new Card('[data-component="card"]');
Подозреваю, что виноват ".parents()"
.closest('.block')
или .parent().next('.slider-btns')
. <input type="text" value="0.00">
document.querySelector('input').addEventListener('keypress', function(e) {
e.preventDefault();
this.value = this.value.replace(/(0)(?!.*\1)/, e.key);
});
const index = arr.findIndex(n => n.id === id);
if (index !== -1) {
arr.splice(index, 1);
}
const newArr = arr.filter(n => n.id !== id);
for (let i = 0; i < arr.length; i++) {
if (arr[i].id === id) {
for (let j = i--; ++j < arr.length; arr[j - 1] = arr[j]) ;
arr.pop();
}
}
arr.reduceRight((_, n, i, a) => n.id === id && a.splice(i, 1), null);
arr.splice(0, arr.length, ...arr.filter(n => n.id !== id));
arr.length -= arr.reduce((acc, n, i, a) => (
a[i - acc] = n,
acc + (n.id === id)
), 0);
<input type="range" min="20" max="200" value="100">
<img src="...">
document.querySelector('input').addEventListener('input', function() {
document.querySelector('img').style.filter = `brightness(${this.value}%)`;
});
const containerSelector = '.section .block';
const itemSelector = '.imgs img';
const $containers = $(containerSelector);
$(itemSelector).each((i, n) => $containers.eq(i).append(n));
// или
const items = document.querySelectorAll(itemSelector);
document.querySelectorAll(containerSelector).forEach((n, i) => n.append(items[i]));
return this.data();
const chunked = (data, chunkSize) =>
Array.prototype.reduce.call(
data,
(acc, n, i) => (
i = i / chunkSize | 0,
(acc[i] = acc[i] || []).push(n),
acc
),
[]
);
console.log(chunked([...Array(10).keys()], 3));
console.log(chunked('ABCDEFG', 2));
console.log(chunked(document.querySelectorAll('img'), 5));
for (let i = 0; i < answer.length - 1; i++)
for (let i = 0; i < words.length; i++)
p.style.color = colors[i]
. function throttle(f, delay) {
let lastCall = -Infinity;
return function() {
const now = +new Date;
if (now - lastCall > delay) {
lastCall = now;
return f.apply(this, arguments);
}
};
}
Object.entries(props).filter(n => n[1]).map(n => n[0]).join(',')
// или
`${Object.entries(props).reduce((acc, [ k, v ]) => (v && acc.push(k), acc), [])}`
// или
Object.entries(props).map(n => n[1] && n[0]).filter(Boolean).toString()
// или
'' + Object.keys(props).reduce((acc, n) => props[n] ? [ ...acc, n ] : acc, [])
// или
String(Object.keys(props).filter(n => props[n]))
const key = 'background-color';
const values = [ 'red', 'lime', 'yellow', 'aqua', 'brown', 'magenta' ];
const getValue = i => values[i % values.length];
const selector = '.item';
$(selector).css(key, getValue);
// или
document.querySelectorAll(selector).forEach((n, i) => {
n.style[key] = getValue(i);
// или
n.style.setProperty(key, getValue(i));
// или
n.style.cssText += `${key}: ${getValue(i)}`;
// или
n.setAttribute('style', key + ': ' + getValue(i));
});
this.sliderPoint.on('click', e => {
this.currentImg = e.target.dataset.id;
this.scrollImages(this.currentImg, this.speed);
});
[...Array(count)]
.map((n, i) => `<div class="slider-point" data-id="${i}"></div>`)
.join('')
// или
''.concat(...Array.from(
{ length: count },
(n, i) => '<div class="slider-point" data-id="' + i + '"></div>'
))
Array(count).fill('<div class="slider-point"></div>').join('')
// или
Array(-~count).join('<div class="slider-point"></div>')
// или
'<div class="slider-point"></div>'.repeat(count)
this.currentImg = $(e.target).index();
// или
this.currentImg = [...e.target.parentNode.children].indexOf(e.target);
document.addEventListener('click', e => {
const slide = e.target.closest('.slide');
if (slide) {
const index = Array.prototype.indexOf.call(
slide.parentNode.children,
slide
);
console.log(index);
}
});
for (const n of document.querySelectorAll('.slide')) {
n.addEventListener('click', onClick);
}
function onClick() {
let index = 0;
for (let el = this; el = el.previousElementSibling; index++) ;
console.log(index);
}
document.querySelectorAll('.slider').forEach(function({ children }) {
[].forEach.call(children, (n, i) => {
n.dataset.index = i;
n.addEventListener('click', this);
});
}, e => console.log(+e.currentTarget.dataset.index));