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');
const containerSelector = '.calculator_price';
const itemSelector = '.calculator_price__item';
const count = 6;
const key = 'display';
const val = 'flex';
$(containerSelector)
.find(`${itemSelector}:lt(${count})`)
.css({ [key]: val });
$(`${containerSelector} ${itemSelector}`)
.filter((i, n) => $(n).index() < count)
.css(key, val);
for (const n of document.querySelectorAll(containerSelector)) {
for (const m of [...n.querySelectorAll(itemSelector)].slice(0, count)) {
m.style.setProperty(key, val);
}
}
document.querySelectorAll(containerSelector).forEach(n => {
n.querySelectorAll(itemSelector).forEach((m, i) => {
m.style[key] = i < count ? val : '';
});
});
<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();
}
});
const { classList: cl } = div;
const toRemove = '_key-word';
cl.remove(...[...cl].filter(n => n.indexOf(toRemove) !== -1));
// или
for (let i = cl.length; i--;) {
if (cl[i].includes(toRemove)) {
cl.remove(cl[i]);
}
}
// или
cl.value = cl.value
.replace(RegExp(`\\S*${toRemove}\\S*`, 'g'), '')
.replace(/^ +| +(?= |$)/g, '');
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);
})();
Math.max(...list)
Работает все ок...
function max(data, key = n => n) {
const getVal = key instanceof Function ? key : n => n[key];
let result = void 0;
for (const n of data) {
const val = getVal(n);
result = result && result[1] > val ? result : [ n, val ];
}
return result && result[0];
}
max([ 12, 9, -47, 83, 22 ]) // 83
max('240815', Number) // '8'
max(document.images, 'width') // не знаю, что выдаст вам, сами посмотрите
max((function*() {
yield 'aaa';
yield 'b';
yield 'cccc';
yield 'ddd';
})(), n => n.length) // 'cccc'
|о|д|и|н|
|е|т|ы|д|
|ч|е|р|в|
|и|р|т|а|
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
*/
const elems = document.querySelectorAll('div');
function handler() {
alert(123);
elems.forEach(n => n.removeEventListener('click', handler));
}
elems.forEach(n => n.addEventListener('click', handler));
const elems = [...document.querySelectorAll('div')];
document.addEventListener('click', function handler(e) {
if (elems.some(n => n.contains(e.target))) {
alert(123);
document.removeEventListener('click', handler);
}
});