Количество LI элементов заранее не известно, поэтому выполнять функцию WHEN с определенным количеством аргументов нет возможности.
$.when(...$('li').get().map(n => $.ajax())).then(/* выполняйте, чо там вам надо */);
this.data = new Proxy(...
сделайтеreturn new Proxy(this, {
get(target, name) {
return name in target
? target[name]
: `Свойства ${name} нет`
},
});
for (const [ k, v ] of new URLSearchParams(url.replace(/.*\?/, ''))) {
document.querySelector(`#${k}`).value = v;
}
url
.slice(url.indexOf('?') + 1)
.split('&')
.map(n => n.split('='))
.forEach(n => document.getElementById(n[0]).value = n[1]);
const data = [...url.split('?').pop().matchAll(/([^&]+)=([^&]*)/g)];
for (let i = 0; i < data.length; i++) {
document.querySelector(`[id="${data[i][1]}"]`).value = data[i][2];
}
.hover {
opacity: 0.5;
}
const map = document.querySelector('.map svg');
map.addEventListener('mouseover', onHover);
map.addEventListener('mouseout', onHover);
function onHover({ target: t }) {
this.querySelectorAll('.hover-effect').forEach(n => {
n.classList.toggle('hover', n !== t && this !== t);
});
}
Если выводить в консоль то коллекции не чем не отличаются.
HTMLCollection
, хранящая элементы DOM, является динамической. При изменении документа она моментально отражает все произведённые изменения.
$('.scheme-wrap polygon').click(function() {
$(`.scheme-wrap__item[data-id="${this.dataset.id}"] .scheme-wrap__popup`).show();
});
.scheme-wrap__popup.visible {
display: block;
}
document.querySelectorAll('.scheme-wrap polygon').forEach(n => {
n.addEventListener('click', onClick);
});
function onClick({ currentTarget: { dataset: { id } } }) {
const selector = `.scheme-wrap__item[data-id="${id}"] .scheme-wrap__popup`;
const popup = document.querySelector(selector);
if (popup) {
popup.classList.add('visible');
}
}
document.addEventListener('click', function(e) {
if (e.target.classList.contains('sbros')) {
for (let el = e.target; el = el.previousElementSibling;) {
if (el.classList.contains('fields')) {
el.value = el.getAttribute('default-value');
break;
}
}
}
});
document.addEventListener('click', ({ target: t }) => {
if (t.matches('.sbros')) {
const el = t.closest('здесь селектор обёртки').querySelector('.fields');
el.value = el.getAttribute('default-value');
}
});
const inputs = document.querySelectorAll('.fields');
const buttons = [...document.querySelectorAll('.sbros')];
buttons.forEach(n => n.addEventListener('click', onClick));
function onClick(e) {
const el = inputs[buttons.indexOf(e.target)];
el.value = el.getAttribute('default-value');
}
const chunked = (arr, chunkSize) =>
Array.from(
{ length: Math.ceil(arr.length / chunkSize) },
(n, i) => arr.slice(i * chunkSize, (i + 1) * chunkSize)
);
const arr = chunked(Object.entries(obj), 10).map(Object.fromEntries);
event.target.tagName != 'button'
В XML (и XML-подобных языках) возвращаемое значение будет в нижнем регистре, а в HTML - в верхнем.
event.target.classList.contains('remove-button')
// или
event.target.matches('.remove-button')
$('.show').click(function() {
$(this).hide().next('.var').slideDown().find('.hide').show();
});
$('.hide').click(function() {
$(this).hide().closest('.var').slideUp().prev('.show').show();
});
rating = Math.max(1, Math.ceil((e.clientX - горизонтальнаяКоординатаЭлемента) / ширинаЭлемента * 5))
const html = [...Array(3)].map((n, i) => `<div class="count">${++i}</div>`).join('');
document.querySelectorAll('.counts').forEach(n => n.insertAdjacentHTML('beforeend', html));
const counts = document.querySelectorAll('.counts');
for (let i = 0; i < counts.length; i++) {
for (let j = 0; j < 3; j++) {
const el = document.createElement('div');
el.className = 'count';
el.innerText = j + 1;
counts[i].appendChild(el);
}
}
const div = document.createElement('div');
div.classList.add('count');
const fragment = document.createDocumentFragment();
fragment.append(...Array.from({ length: 3 }, (n, i) => (
n = div.cloneNode(),
n.textContent = -~i,
n
)));
for (const n of document.querySelectorAll('.counts')) {
n.append(fragment.cloneNode(true));
}
const siblings = Array.prototype.filter.call(
this.parentNode.children,
n => n !== this
);
const siblings = [];
for (let el = this; (el = el.previousElementSibling); siblings.unshift(el)) ;
for (let el = this; (el = el.nextElementSibling); siblings.push(el)) ;