const buttonSelector = '.one-click-button';
const dataAttr = 'id';
const input = document.querySelector('#one_b_id');
const getId = el => el.dataset[dataAttr];
// или
const getId = el => el.getAttribute(`data-${dataAttr}`);
// или
const getId = el => el.attributes['data-' + dataAttr].value;
document.addEventListener('click', e => {
const button = e.target.closest(buttonSelector);
if (button) {
input.value = getId(button);
}
});
document.querySelectorAll(buttonSelector).forEach(function(n) {
n.addEventListener('click', this);
}, e => input.value = getId(e.currentTarget));
Как я могу проверить на наличие в начале строки undefined и удалить её?
str.replace(/^undefined/, '')
. divParent.insertBefore(elems[i].cloneNode(true), null);
divParent.insertBefore(elems[i].cloneNode(true), divParent.firstChild);
// или
divParent.insertAdjacentElement('afterbegin', elems[i].cloneNode(true));
// или
divParent.prepend(elems[i].cloneNode(true));
for (const n of document.getElementsByClassName('basic')) {
n.after(document.createElement('div'));
n.nextSibling.classList.add('parent');
n.nextSibling.append(document.createElement('b'));
n.nextSibling.lastChild.textContent = 'Важная строка';
n.nextSibling.prepend(n);
}
document.querySelectorAll('.basic').forEach(n => {
const html = `
<div class="parent">
${n.outerHTML}
<b>Важная строка</b>
</div>
`;
n.outerHTML = html;
// или
n.insertAdjacentHTML('afterend', html);
n.remove();
// или
n.replaceWith(document.createRange().createContextualFragment(html));
// или
n.replaceWith(...new DOMParser().parseFromString(html, 'text/html').body.childNodes);
});
document.querySelectorAll('.number').forEach(function(n) {
const top = n.getBoundingClientRect().top;
const end = +n.dataset.max;
window.addEventListener('scroll', function onScroll() {
if (window.pageYOffset > top - window.innerHeight / 2) {
this.removeEventListener('scroll', onScroll);
let start = +n.innerHTML;
const interval = setInterval(function() {
n.innerHTML = ++start;
if (start === end) {
clearInterval(interval);
}
}, 5);
}
});
});
JSON.parse
- вы же указали dataType: 'json'
в настройках запроса, так что data уже будет объектом. const max = Math.max(...Object.values(obj[0]).map(n => n ? n.level : -Infinity));
const objectParser = (data, path = '') => Object.assign({
[path || '<root>']: data,
}, ...(data instanceof Object
? Object.entries(data).map(([ k, v ]) => objectParser(v, `${path}[${k}]`))
: []
));
const parentSelector = 'селектор родительского элемента';
const noClassSelector = 'селектор элементов, которым не надо добавлять класс';
const className = 'класс';
$(parentSelector)
.children()
.removeClass(className)
.not(noClassSelector)
.addClass(className);
// или
for (const n of document.querySelector(parentSelector).children) {
n.classList.toggle(className, !n.matches(noClassSelector));
}
n < locals.length
сделайте n < locals.length / 3
.3 * n
будет просто n
, вместо 3 * n + 1
будет n + 1
, и т.д. <component :is="item.type" :data="item.data"></component>
const myPlacemark = new ymaps.Placemark([ 59.8755, 30.3955 ], {
hintContent: '<b>Адрес/метро:</b> Белы Куна 21-Н, 1й этаж/ м.Международная <br> <b>Площадь:</b> 45 м <br> <b>Стоимость:</b> 70 000 р. в мес. + К/У' ,
iconContent: 'Магазин (нежилой фонд)'
}, {
preset: 'islands#darkGreenStretchyIcon'
});
const formatTime = seconds => [
seconds / 3600 | 0,
seconds / 60 % 60 | 0,
seconds % 60,
].map(n => `${n}`.padStart(2, 0)).join(':');