const exclude = [ 'class1', 'class2', 'class3' ];
Array
.from(document.querySelectorAll('div a'))
.filter(n => !exclude.some(m => n.classList.contains(m)))
.forEach(n => n.style.display = 'none');
const $select = $('select').change(function() {
const selected = $(':selected', this).get().map(n => n.value);
const disabled = $(':disabled', this).get().map(n => n.value);
$select
.not(this)
.find('option')
.prop('disabled', function(i, val) {
return disabled.includes(this.value) ? val : selected.includes(this.value);
});
});
const selects = [...document.querySelectorAll('select')];
selects.forEach(n => n.addEventListener('change', onChange));
function onChange() {
const options = [...this];
const selected = options.filter(n => n.selected).map(n => n.value);
const disabled = options.filter(n => n.disabled).map(n => n.value);
for (const n of selects.flatMap(m => m === this ? [] : [...m])) {
n.disabled = disabled.includes(n.value) ? n.disabled : selected.includes(n.value);
}
}
class="subs"
, например.$('select').change(function() {
$('.subs [class*="sub"]')
.addClass('hidden')
.filter(`.sub${$(this).val()}`)
.removeClass('hidden');
});
// или
$('select').change(function(e) {
const index = ($(e.target).prop('selectedIndex') || Infinity) - 1;
this.addClass('hidden');
this.eq(index).removeClass('hidden');
}.bind($('.subs').children()));
document.querySelector('select').addEventListener('change', e => {
const cls = `sub${e.target.value}`;
document.querySelectorAll('.subs [class*="sub"]').forEach(n => {
n.classList.toggle('hidden', !n.classList.contains(cls));
});
});
// или
document.querySelector('select').addEventListener('change', e => {
const index = ~-e.target.selectedIndex;
Array.prototype.forEach.call(
document.querySelector('.subs').children,
(n, i) => n.classList.toggle('hidden', i !== index)
);
});
const input = document.querySelector('input');
const num = 100;
input.addEventListener('change', function() {
this.value = (this.value / num | 0) * num;
});
<button data-step="-1">-</button>
<button data-step="+1">+</button>
document.querySelectorAll('[data-step]').forEach(function(n) {
n.addEventListener('click', this);
}, e => input.value = +input.value + e.target.dataset.step * num);
const inputs = document.querySelectorAll('ol input');
const classes = [...new Set(Array.prototype.flatMap.call(
inputs,
n => [...n.classList]
))];
const classes = Array
.from(inputs, n => Array.from(n.classList))
.flat();
class deferred {
constructor() {
this.promise = new Promise(resolve => this.resolve = resolve);
}
then(f) {
this.promise = this.promise.then(f);
}
}
class deferred {
constructor() {
this.callbacks = [];
}
then(f) {
this.callbacks.push(f);
}
resolve(val) {
this.callbacks.reduce((res, f) => f(res), val);
}
}
Не получается обратиться к элементу по его id.
const svgDocument = document.querySelector('object').contentDocument;
const element = svgDocument.querySelector('здесь указываете нужный id или что там вам надо');
textareaEl.addEventListener('paste', function() {
setTimeout(() => {
this.value = this.value.split('\n').map((n, i) => {
const line = i + 1;
return `${line}. ${n.replace(RegExp(`^${line}\\. `), '')}`;
}).join('\n');
});
});
Элемент - представленный в примере - это FileInput (кликать нужно на него, что бы вызвать диалог выбора файлов)
const imgs = document.querySelectorAll('img[src*="/test/"]');
это не в dom, а в строке
const div = document.createElement('div');
div.innerHTML = str;
const imgsStr = ''.concat(...[].map.call(
div.querySelectorAll('img[src*="/test/"]'),
n => n.outerHTML
));
const imgsStr = Array
.from(
new DOMParser().parseFromString(str, 'text/html').querySelectorAll('img[src*="/test/"]'),
n => n.outerHTML)
.join('');
const imgsStr = Array.prototype.reduce.call(
document.createRange().createContextualFragment(str).querySelectorAll('img[src*="/test/"]'),
(acc, n) => acc + n.outerHTML,
''
);
мне нужно удалить из этой строки все картинки, которые...
const div = document.createElement('div');
div.innerHTML = str;
const imgs = div.querySelectorAll('img[src*="/test/"]');
for (let i = 0; i < imgs.length; i++) {
imgs[i].outerHTML = '';
}
str = div.innerHTML;
const { body } = new DOMParser().parseFromString(str, 'text/html');
body.querySelectorAll('img[src*="/test/"]').forEach(n => n.remove());
str = body.innerHTML;
const fragment = document.createRange().createContextualFragment(str);
for (const n of fragment.querySelectorAll('img[src*="/test/"]')) {
n.parentNode.removeChild(n);
}
str = Array.from(fragment.childNodes, n => n.outerHTML || n.nodeValue).join('');
function onlyOne({ value, dataset: { group } }) {
document.querySelectorAll(`[data-group="${group}"]`).forEach(n => {
n.checked = n.checked && n.value === value;
});
}
todoList.forEach(function(element) {
document.getElementById('out').innerHTML += '<p>'+element+'</p>';
});
document.getElementById('out').innerHTML = todoList.map(n => `<p>${n}</p>`).join('');
const div = document.createElement('div');
div.innerHTML = html;
div.querySelectorAll('a[href*="/test/"]').forEach(n => n.outerHTML = '');
html = div.innerHTML;
const { body } = new DOMParser().parseFromString(html, 'text/html');
body.querySelectorAll('a[href*="/test/"]').forEach(n => n.remove());
html = body.innerHTML;
const fragment = document.createRange().createContextualFragment(html);
fragment.querySelectorAll('a[href*="/test/"]').forEach(n => n.parentNode.removeChild(n));
html = Array.from(fragment.childNodes, n => n.outerHTML || n.nodeValue).join('');
function sort(desc, attr) {
const select = document.querySelector('#search-sort-field');
select.append(...Array
.from(select.children, n => [ n, n.getAttribute(attr) * (desc ? 1 : -1) ])
.sort((a, b) => a[1] - b[1])
.map(n => n[0])
);
}
const tbody = document.querySelector('#table tbody');
// или
const [ tbody ] = document.getElementById('table').tBodies;
tbody.innerHTML = arr
.map(n => `
<tr>
<td><a href="${n.link}">${n.city}</a></td>
<td>${n.country}</td>
</tr>`)
.join('');
for (const n of arr) {
const tr = tbody.insertRow();
const a = document.createElement('a');
a.href = n.link;
a.text = n.city;
tr.insertCell().append(a);
tr.insertCell().textContent = n.country;
}