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'));
str.trim().replace(/\s{2,}/g, ' ')
str.split(' ').filter(Boolean).join(' ')
(str.match(/\S+/g) || []).join` `
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;
}
$('.container').find('h2, h3').each(function(i) {
text = $('.select-list li a').eq(i).text();
не надо код, подскажите алгоритм
function shorten(val) {
if (val <= 10000) {
return val.toString();
}
const thousands = val / 1000;
const rounded = Math.round(thousands);
const deviation = Math.sign(thousands - rounded);
return `${[ '≈', '', '>' ][deviation + 1]}${rounded} т.`;
}
Цена завернута в див price prc-new
$('[name="variant"] option:checked').data('price')
$('.price.prc-new span').text()
$('input').on('input', function() {
$(this).val((i, v) => Math.max(this.min, Math.min(this.max, v)));
});
document.querySelector('input').addEventListener('input', ({ target: t }) => {
t.value = Math.max(t.min, Math.min(t.max, t.value));
});
$('.price').text((i, text) => {
const [ price, currency ] = text.split(' ');
return `${(+price).toLocaleString()} ${currency}`;
});
// или
document.querySelectorAll('.price').forEach(n => {
n.textContent = n.textContent.replace(/\d(?=(\d{3})+\D)/g, '$& ');
});
function toggleClasses(block) {
const info = block.querySelector('.infoWrapper');
block.classList.toggle('marginBottom10');
block.classList.toggle('marginBottom120');
info.classList.toggle('displayNone');
info.classList.toggle('displayBlock');
}
document.addEventListener('click', function(e) {
const block = e.target.closest('.blockSvg');
if (block) {
const prevBlock = document.querySelector('.blockSvg.marginBottom120');
if (prevBlock && prevBlock !== block) {
toggleClasses(prevBlock);
}
toggleClasses(block);
}
});
const className = 'dop_atr';
const maxlen = 250;
text
функцию - вызывается для каждого элемента набора, принимает текущее текстовое содержимое элемента и возвращает новое:$(`.${className}`).text((i, text) => {
return text[maxlen] ? `${text.substring(0, maxlen)}...` : text;
});
for (const n of document.getElementsByClassName(className)) {
n.innerText = n.innerText.slice(0, maxlen) + (n.innerText.charAt(maxlen) && '...');
}
// или
document.querySelectorAll(`.${className}`).forEach(function(n) {
n.textContent = n.textContent.replace(this, '$1...');
}, RegExp(`(.{${maxlen}}).+`));
document.addEventListener('click', function(e) {
const el = document.getElementById(e.target.dataset.id);
if (el) {
el.checked = !el.checked;
}
});
const buttons = document.querySelectorAll('[data-id]');
const onClick = e => checkboxes[e.target.dataset.id].checked ^= 1;
const checkboxes = Array
.from(buttons, n => n.dataset.id)
.reduce((acc, n) => (acc[n] = document.querySelector(`#${n}`), acc), {});
buttons.forEach(n => n.addEventListener('click', onClick));
$('.tab__list').on('click', '.tab__item', function() {
const
$this = $(this),
{ label, ...attrs } = text[$this.data('tab')];
$this.siblings().removeClass('tab__item--active');
$this.addClass('tab__item--active');
$('#formField').attr(attrs);
$('#formLabel').text(label);
});
.red{border:3px solid red;;}
.green{border:3px solid red;;}
.items-list-item-colors label {
border: 3px solid red;
}
.items-list-item-colors label.active {
border-color: green;
}
class="red"
вырезаете.const containerSelector = '.items-list-item-colors';
const itemSelector = 'label';
const activeClass = 'active';
$(containerSelector).change(function() {
$(itemSelector, this)
.removeClass(activeClass)
.has(':checked')
.addClass(activeClass);
});
// или
document.querySelectorAll(containerSelector).forEach(n => {
n.addEventListener('change', onChange);
});
function onChange({ target: t }) {
this.querySelectorAll(itemSelector).forEach(n => {
n.classList.toggle(activeClass, n.contains(t));
});
}
// или
document.addEventListener('change', e => {
const item = e.target.closest(itemSelector);
if (item) {
item.closest(containerSelector).querySelectorAll(itemSelector).forEach(n => {
n.classList.toggle('active', n === item);
});
}
});