document.querySelector('link[href*="light"]').disabled=true;
document.head.insertAdjacentHTML('beforeend', '<link rel="stylesheet" type="text/css" href="css/main.dark.css">');
function getTheme() {
return localStorage.getItem('theme') || 'light'; // default light
}
function toggleTheme() {
let newTheme, oldTheme;
if (getTheme() === 'light') {
newTheme = 'dark';
oldTheme = 'light';
} else {
newTheme = 'light';
oldTheme = 'dark';
}
document.querySelector(`link[href*="${oldTheme}"]`).disabled=true;
document.head.insertAdjacentHTML('beforeend', `<link rel="stylesheet" type="text/css" href="css/main.${newTheme}.css">`);
}
$(window).on('click', function(evt) { // слушаем все клики
if (!evt.target.closest('.search-wrapper')) { // если клик не внутри .search-wrapper
$('.search-wrapper').hide(); // скрываем
}
});
$('.language-chooser li').on('click', function(evt) {
evt.stopPropagation();
evt.preventDefault();
$('.language-chooser li').show(); // show all languages
});
$(document).on('click', function(evt) {
$('.language-chooser li:not(.active)').hide(); // hide inactive languages
});
const data = {
'hello': 42,
'world': 100,
'foo': 'bar',
};
const keys = Object.keys(data);
console.log(...keys); // -> hello world foo
console.log(keys[1]); // -> world
// также через for ... in можно перебрать перечислимые свойства
for (let key in data) {
console.log(key, data[key]); // -> hello 42
// -> world 100
// -> foo bar
}
const x = 123.321555;
const rounded = x.toFixed(3); // три знака после запятой
console.log(rounded); // -> 123.321
const tail = rounded - Math.round(x)
console.log(tail); // // -> 0.321
// или регуляркой из строки
const str = x.toString().match(/\.(\d{1,3})/)[1];
console.log(str); // // -> "321"
const url ='https://images.unsplash.com/photo-1599009434802-ca1dd09895e7';
const img = new Image();
img.onload = function() {
console.log('loaded!');
someElement.appendChild(img); // вставить куда-то
}
img.src = url;
behavior: 'smooth'