const childSelector = 'img';
const className = 'myclass';
$(`.${className}:not(:has(> ${childSelector}))`).removeClass(className);
document.querySelectorAll(`.${className}`).forEach(n => {
n.classList.toggle(className, !!n.querySelector(`:scope > ${childSelector}`));
});
const elems = document.getElementsByClassName(className);
for (let i = elems.length; i--;) {
if (!Array.prototype.some.call(elems[i].children, n => n.matches(childSelector))) {
elems[i].classList.remove(className);
}
}
reader.onload = function(e) {
console.log(this.result);
};
reader.readAsText(file);
[...document.getElementsByClassName('cls')].reduce((max, n) => {
const val = +n.innerText;
return val > max[1] ? [ n, val ] : max;
}, [ null, -Infinity ])[0].style.color = 'red';
const text = [
'hello, world!!',
'fuck the world',
'fuck everything',
];
$('table').on('click', 'td', function() {
const $this = $(this);
const index = (+$this.data('index') + 1) % text.length;
$this.text(text[index]).data('index', index);
}).find('td').data('index', -1);
document.body.appendChild(anchor)
. А потом уже делать click. function createTree(data, levelKey, childrenKey) {
const tree = [];
data.forEach(n => {
let arr = tree;
for (let level = 0; n[levelKey] > level++;) {
arr = arr[arr.length - 1][childrenKey];
}
arr.push(Object.assign({ [childrenKey]: [] }, n));
});
return tree;
}
const tree = createTree([
{ id: 1, title: 'test1', level: 0 },
{ id: 2, title: 'test2', level: 1 },
{ id: 3, title: 'test3', level: 2 },
{ id: 4, title: 'test4', level: 1 },
{ id: 5, title: 'test5', level: 0 },
], 'level', 'nodes');
$('.calculator_price')
.find('.calculator_price__item:lt(6)')
.css('display', 'flex');
$('.calculator_price .calculator_price__item')
.filter((i, n) => $(n).index() < 6)
.css('display', 'flex');
document.querySelectorAll('.calculator_price').forEach(n => {
[...n.querySelectorAll('.calculator_price__item')]
.slice(0, 6)
.forEach(n => n.style.display = 'flex');
});
<div id="inputs">
<input maxlength="5">
</div>
const $inputs = $('#inputs').on('input', 'input', function() {
const $this = $(this);
const maxlen = +$this.attr('maxlength');
if ($this.val().length === maxlen) {
let $next = $this.next();
if (!$next.length) {
$next = $(`<input maxlength="${maxlen}">`).appendTo($inputs);
}
$next.focus();
}
});
// или
document.querySelector('#inputs').addEventListener('input', function(e) {
const input = e.target;
const maxlen = +input.getAttribute('maxlength');
if (input.value.length === maxlen) {
if (!input.nextElementSibling) {
this.insertAdjacentHTML('beforeend', `<input maxlength="${maxlen}">`);
}
input.nextElementSibling.focus();
}
});
div.classList.remove(...[...div.classList].filter(n => n.indexOf('_key-word') !== -1))
false
, подсовывайте какое-то корректное дефолтное значение, в данном случае - пустую строку:(item._Name || '').trim()
item._Name?.trim?.() ?? ''
Владивосток - плюс 7 часов к текущему.
function getTimeInTimezone(zone) {
const
d = new Date(),
utc = d.getTime() + d.getTimezoneOffset() * 60000;
return new Date(utc + zone * 3600000);
}
new Date()
, делаем так: getTimeInTimezone(10)
. if (![ 'search', 'search_input', 'cancel_btn' ].includes(target.className)) {
const t = target.className;
if (t !== 'search' && t !== 'search_input' && t !== 'cancel_btn') {
if (!target.matches('.search, .search_input, .cancel_btn')) {
<div class="block">hello, world!!</div>
<div class="block">fuck the world</div>
<div class="block">fuck everything</div>
.block {
display: inline-block;
width: 500px;
height: 150px;
padding: 20px;
background: red;
color: white;
}
const $blocks = $('.block').hide();
let i = -1;
(function showNext() {
$blocks
.eq(i = (i + 1) % $blocks.length)
.dequeue()
.fadeIn(1000)
.delay(500)
.fadeOut(1000)
.queue(showNext);
})();
|о|д|и|н|
|е|т|ы|д|
|ч|е|р|в|
|и|р|т|а|
function makeTable(data) {
data = data.join('').split('');
let width = Math.sqrt(data.length) | 0;
let height = (data.length / width) | 0;
while (width * height !== data.length) {
width--;
height = (data.length / width) | 0;
}
const position = [ 0, 0 ];
const directions = [
[ 1, 0 ],
[ 0, 1 ],
[ -1, 0 ],
[ 0, -1 ],
];
let direction = 0;
const result = Array.from({ length: height }, n => Array(width).fill(null));
for (const n of data) {
result[position[1]][position[0]] = n;
if (null !== (result[position[1] + directions[direction][1]] || {})[position[0] + directions[direction][0]]) {
direction = (direction + 1) % directions.length;
}
position[0] += directions[direction][0];
position[1] += directions[direction][1];
}
return result.map(n => n.join(' ')).join('\n');
}
console.log(makeTable([ 'раз', 'два', 'три' ])); /*
р а з
р и д
т а в
*/
console.log(makeTable([ 'один', 'два', 'три', 'четыре' ])); /*
о д и н
е т ы д
ч е р в
и р т а
*/
console.log(makeTable([ 'hello', ',', ' ', 'world', '!!' ])); /*
h e
! l
! l
d o
l ,
r
o w
*/