возможно ли чтобы скрипт создавал другой html-код?
Цитата:
May 2021 <br> <table class="vertical" cellspacing="2" cellpadding="5" border="1" align="center"> <tr> <td> Пн </td> <td> 26 </td> <td> 3 </td> <td> 10 </td> <td> 17 </td> <td> 24 </td> <td> 31 </td> </tr> <tr> <td> Вт </td> <td> 27 </td> <td> 4 </td> <td> 11 </td> <td> 18 </td> <td> 25 </td> <td> 1 </td> </tr> <tr> <td> Ср </td> <td> 28 </td> <td> 5 </td> <td> 12 </td> <td> 19 </td> <td> 26 </td> <td> 2 </td> </tr> <tr> <td> Чт </td> <td> 29 </td> <td> 6 </td> <td> 13 </td> <td> 20 </td> <td> 27 </td> <td> 3 </td> </tr> <tr> <td> Пт </td> <td> 30 </td> <td> 7 </td> <td> 14 </td> <td> 21 </td> <td> 28 </td> <td> 4 </td> </tr> <tr> <td> Сб </td> <td> 1 </td> <td> 8 </td> <td> 15 </td> <td> 22 </td> <td> 29 </td> <td> 5 </td> </tr> <tr> <td> Вс </td> <td> 2 </td> <td> 9 </td> <td> 16 </td> <td> 23 </td> <td> 30 </td> <td> 6 </td> </tr> </table> <br> <table class="gorizontal" cellspacing="2" cellpadding="5" border="1" align="center"> <tr> <td> Пн </td> <td> Вт </td> <td> Ср </td> <td> Чт </td> <td> Пт </td> <td> Сб </td> <td> Вс </td> </tr> <tr> <td> 26 </td> <td> 27 </td> <td> 28 </td> <td> 28 </td> <td> 30 </td> <td> 1 </td> <td> 2 </td> </tr> <tr> <td> 3 </td> <td> 4 </td> <td> 5 </td> <td> 6 </td> <td> 7 </td> <td> 8 </td> <td> 9 </td> </tr> <tr> <td> 10 </td> <td> 11 </td> <td> 12 </td> <td> 13 </td> <td> 14 </td> <td> 15 </td> <td> 16 </td> </tr> <tr> <td> 17 </td> <td> 18 </td> <td> 19 </td> <td> 20 </td> <td> 21 </td> <td> 22 </td> <td> 23 </td> </tr> <tr> <td> 24 </td> <td> 25 </td> <td> 26 </td> <td> 27 </td> <td> 28 </td> <td> 29 </td> <td> 30 </td> </tr> <tr> <td> 31 </td> <td> 1 </td> <td> 2 </td> <td> 3 </td> <td> 4 </td> <td> 5 </td> <td> 6 </td> </tr> </table> вот сам скрипт <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>calendar-rotate</title> <style type="text/css"> #calendar_rotate { border: 1px solid hsl(0,0%,66%); padding: 5px; display: inline-block; width: 373px; } #navigation_panel { border: 1px solid hsl(0,0%,66%); padding: 5px 10px; margin: 0 0 5px 0; display: inline-block; white-space: nowrap; } #background_month { background-color: hsl(210,100%,85%); display: inline-block; } #month_select { background-color: transparent; color: hsl(210,100%,45%); font: 14px serif; border: 1px solid hsl(210,100%,45%); } .month { border: 1px solid hsl(0,0%,66%); display: flex; flex-flow: column wrap; justify-content: space-between; width: 100%; height: 367px; padding: 1px; } .vert { border: 1px solid #ff0; height auto; display: flex; justify-content: space-between; } .vert .week { width: auto; border: 1px solid #090; flex: 1 1 auto; align-self: stretch; } .mont .vert .week .day { height 11.52%; } .month .week { border: 1px solid #00a; flex: 1 1 auto; align-self: stretch; width: 100%; height: auto; } .month .week .day { width 1.947em;; height 1.85em; } .month, .month.vert .week, .day { display: inline-block; width: auto; } .month.vert .day { display: block; width: 1.85em; } .day { border: 1px solid hsl(0,0%,66%); width: 1.964em; line-height: 30px; text-align: center; padding: 3.5px; margin: 1px; background-color: hsl(210,100%,90%); color: hsl(210,100%,50%); } .day:nth-child(7n + 6), .day:nth-child(7n + 7) { background-color: hsl(348,100%,90%); color: hsl(348,100%,50%); } select { width: 109px; height: 29.5px; } input { border: 1px solid hsl(0,0%,66%); width: 54px; height: 25.5px;display: inline-block; text-align: center; } #month_minus, #year_minus { width: 28px; text-align: center; border-radius: 12px 0 0 12px / 10px 0 0 10px; margin: 0 -6px 0 0; position: relative; top: 1.5px; } #month_plus, #year_plus { width: 28px; text-align: center; border-radius: 0 12px 12px 0 / 0 10px 10px 0; margin: 0 0 0 -6px; position: relative; top: 1.5px; } #month_minus, #month_plus, #year_minus, #year_plus, #year_input { background-color: hsl(210,100%,85%); color: hsl(210,100%,45%); font: 14px serif; border: 1px solid hsl(210,100%,45%);} </style> </head> <body> <div id="calendar_rotate"> <div id="navigation_panel"> <button id="month_minus">◀</button> <span id="background_month"><select id="month_select"></select></span> <button id="month_plus">▶</button> <button id="year_minus">◀</button> <input id="year_input" type="number" size="4" value=""/> <button id="year_plus">▶</button> <button id="month_rotate">➙</button> </div> <div class="month vert"></div> <script> var calendar = { update: function(year, month) { this.days.length = 7; var stepDay = new Date(year, month, 1); stepDay.setDate(stepDay.getDate() + 0 - ((stepDay.getDay() + 6) % 7)); var lastDay = new Date(year, month + 1, 0); lastDay.setDate(lastDay.getDate() + 6 - ((lastDay.getDay() + 6) % 7)); while (stepDay <= lastDay) { this.days.push(stepDay.getDate()); stepDay.setHours(24); } }, render: function() { var html = ''; for (var i = 0, j = 0; i < this.days.length; j = ++i % 7) { if (j == 0) html += '<div class="week">'; html += '<div class="day">' + this.days[i] + '</div>'; if (j == 6) html += '</div>'; } this.element.innerHTML = html; }, toggle: function() { this.element.classList.toggle('vert'); }, }; var today = new Date(), thisYear = today.getFullYear(), thisMonth = today.getMonth(); calendar.days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; selector = document.getElementById('month_select'); month_list = ['Январь', 'Февраль', 'Март', 'Апрель', ' Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']; for (let i = 0; i < month_list.length; i++){ selector.options[i] = new Option(month_list[i], i); } selector.selectedIndex = thisMonth; selector.addEventListener('change', load); document.getElementById('year_input').addEventListener('change', load); calendar.element = document.querySelector('.month'); document.getElementById('month_rotate').addEventListener('click', function() { calendar.toggle(); }); function load(){ let year = document.getElementById('year_input').value; let month = selector.selectedIndex; calendar.update(year, month); calendar.render(); } document.addEventListener("DOMContentLoaded", ()=>{ document.getElementById('year_input').value = thisYear; load(); }); </script> </div> </body> </html> |
скрипт создаёт html-код состоящий из дивов, а как сделать чтобы вместо дивов создавались таблицы?
|
Цитата:
На этом принципе и делаются те же React.JS или Vue.JS... Цитата:
2. Начать делать таблицы. |
ksa,
и как сделать таблицы? |
Цитата:
<div id='root'></div> <script> const or = document.querySelector('#root'); const ot = document.createElement('table'); for (let i = 0; i < 3; i++) { let tr = document.createElement('tr'); for (let j = 0; j < 3; j++) { let td = document.createElement('td'); td.innerHTML = i + ' - ' + j; tr.appendChild(td); } ot.appendChild(tr); } or.appendChild(ot); </script> |
Цитата:
document.createElement('table') insertRow insertCell http://javascript.ru/forum/dom-windo...tablicy-2.html |
рони,
sorri, но моих познаний в js не достаточно чтобы это сделать :-( |
Цитата:
|
Блондинка,
месяц в таблице, найди отличия))) <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>calendar-rotate</title> <style type="text/css"> #calendar_rotate { border: 1px solid hsl(0,0%,66%); padding: 5px; display: inline-block; width: 373px; } #navigation_panel { border: 1px solid hsl(0,0%,66%); padding: 5px 10px; margin: 0 0 5px 0; display: inline-block; white-space: nowrap; } #background_month { background-color: hsl(210,100%,85%); display: inline-block; } #month_select { background-color: transparent; color: hsl(210,100%,45%); font: 14px serif; border: 1px solid hsl(210,100%,45%); } .month { border: 1px solid hsl(0,0%,66%); display: flex; flex-flow: column wrap; justify-content: space-between; width: 100%; height: 367px; padding: 1px; } .vert { border: 1px solid #ff0; height auto; display: flex; justify-content: space-between; } .vert .week { width: auto; border: 1px solid #090; flex: 1 1 auto; align-self: stretch; } .mont .vert .week .day { height 11.52%; } .month .week { border: 1px solid #00a; flex: 1 1 auto; align-self: stretch; width: 100%; height: auto; } .month .week .day { width 1.947em;; height 1.85em; } .month, .month.vert .week, .day { display: inline-block; width: auto; } .month.vert .day { display: block; width: 1.85em; } .day { border: 1px solid hsl(0,0%,66%); width: 1.964em; line-height: 30px; text-align: center; padding: 3.5px; margin: 1px; background-color: hsl(210,100%,90%); color: hsl(210,100%,50%); } .day:nth-child(7n + 6), .day:nth-child(7n + 7) { background-color: hsl(348,100%,90%); color: hsl(348,100%,50%); } select { width: 109px; height: 29.5px; } input { border: 1px solid hsl(0,0%,66%); width: 54px; height: 25.5px;display: inline-block; text-align: center; } #month_minus, #year_minus { width: 28px; text-align: center; border-radius: 12px 0 0 12px / 10px 0 0 10px; margin: 0 -6px 0 0; position: relative; top: 1.5px; } #month_plus, #year_plus { width: 28px; text-align: center; border-radius: 0 12px 12px 0 / 0 10px 10px 0; margin: 0 0 0 -6px; position: relative; top: 1.5px; } #month_minus, #month_plus, #year_minus, #year_plus, #year_input { background-color: hsl(210,100%,85%); color: hsl(210,100%,45%); font: 14px serif; border: 1px solid hsl(210,100%,45%);} </style> </head> <body> <div id="calendar_rotate"> <div id="navigation_panel"> <button id="month_minus">◀</button> <span id="background_month"><select id="month_select"></select></span> <button id="month_plus">▶</button> <button id="year_minus">◀</button> <input id="year_input" type="number" size="4" value=""/> <button id="year_plus">▶</button> <button id="month_rotate">➙</button> </div> <table class="month vert"></table> <script> var calendar = { update: function(year, month) { this.days.length = 7; var stepDay = new Date(year, month, 1); stepDay.setDate(stepDay.getDate() + 0 - ((stepDay.getDay() + 6) % 7)); var lastDay = new Date(year, month + 1, 0); lastDay.setDate(lastDay.getDate() + 6 - ((lastDay.getDay() + 6) % 7)); while (stepDay <= lastDay) { this.days.push(stepDay.getDate()); stepDay.setHours(24); } }, render: function() { var html = ''; for (var i = 0, j = 0; i < this.days.length; j = ++i % 7) { if (j == 0) html += '<tr class="week">'; html += '<td class="day">' + this.days[i] + '</td>'; if (j == 6) html += '</td>'; } this.element.innerHTML = html; }, toggle: function() { this.element.classList.toggle('vert'); }, }; var today = new Date(), thisYear = today.getFullYear(), thisMonth = today.getMonth(); calendar.days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; selector = document.getElementById('month_select'); month_list = ['Январь', 'Февраль', 'Март', 'Апрель', ' Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']; for (let i = 0; i < month_list.length; i++){ selector.options[i] = new Option(month_list[i], i); } selector.selectedIndex = thisMonth; selector.addEventListener('change', load); document.getElementById('year_input').addEventListener('change', load); calendar.element = document.querySelector('.month'); document.getElementById('month_rotate').addEventListener('click', function() { calendar.toggle(); }); function load(){ let year = document.getElementById('year_input').value; let month = selector.selectedIndex; calendar.update(year, month); calendar.render(); } document.addEventListener("DOMContentLoaded", ()=>{ document.getElementById('year_input').value = thisYear; load(); }); </script> </div> </body> </html> |
блин, вопрос по css, как и вертикальную и горизонтальную таблицу растянуть на всю ширину и высоту?
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>calendar-rotate</title> <style type="text/css"> #calendar_rotate { border: 1px solid hsl(0,0%,66%); padding: 5px; display: inline-block; width: 373px; } #navigation_panel { border: 1px solid hsl(0,0%,66%); padding: 5px 10px; margin: 0 0 5px 0; display: inline-block; white-space: nowrap; } #background_month { background-color: hsl(210,100%,85%); display: inline-block; } #month_select { background-color: transparent; color: hsl(210,100%,45%); font: 14px serif; border: 1px solid hsl(210,100%,45%); } .month { border: 1px solid hsl(0,0%,66%); display flex; flex-flow column wrap; justify-content space-between; width: 367px; height: 367px; padding: 1px; } .vert { border: 1px solid #999; width: 367px; height: 367px; display flex; justify-content space-between; } .vert .week { flex 1 1 auto; align-self stretch; } .month .week { flex 1 1 auto; align-self stretch; } .month, .month.vert .week, .day { display: table-cell; } .month.vert .day { display: table-row; } .day { border: 1px solid hsl(0,0%,66%); text-align: center; padding: 3.5px; background-color: hsl(210,100%,90%); color: hsl(210,100%,50%); } .day:nth-child(7n + 6), .day:nth-child(7n + 7) { background-color: hsl(348,100%,90%); color: hsl(348,100%,50%); } select { width: 109px; height: 29.5px; } input { border: 1px solid hsl(0,0%,66%); width: 54px; height: 25.5px;display: inline-block; text-align: center; } #month_minus, #year_minus { width: 28px; text-align: center; border-radius: 12px 0 0 12px / 10px 0 0 10px; margin: 0 -6px 0 0; position: relative; top: 1.5px; } #month_plus, #year_plus { width: 28px; text-align: center; border-radius: 0 12px 12px 0 / 0 10px 10px 0; margin: 0 0 0 -6px; position: relative; top: 1.5px; } #month_minus, #month_plus, #year_minus, #year_plus, #year_input { background-color: hsl(210,100%,85%); color: hsl(210,100%,45%); font: 14px serif; border: 1px solid hsl(210,100%,45%);} </style> </head> <body> <div id="calendar_rotate"> <div id="navigation_panel"> <button id="month_minus">◀</button> <span id="background_month"><select id="month_select"></select></span> <button id="month_plus">▶</button> <button id="year_minus">◀</button> <input id="year_input" type="number" size="4" value=""/> <button id="year_plus">▶</button> <button id="month_rotate">➙</button> </div> <table class="month vert"></table> <script> var calendar = { update: function(year, month) { this.days.length = 7; var stepDay = new Date(year, month, 1); stepDay.setDate(stepDay.getDate() + 0 - ((stepDay.getDay() + 6) % 7)); var lastDay = new Date(year, month + 1, 0); lastDay.setDate(lastDay.getDate() + 6 - ((lastDay.getDay() + 6) % 7)); while (stepDay <= lastDay) { this.days.push(stepDay.getDate()); stepDay.setHours(24); } }, render: function() { var html = ''; for (var i = 0, j = 0; i < this.days.length; j = ++i % 7) { if (j == 0) html += '<tr class="week">'; html += '<td class="day">' + this.days[i] + '</td>'; if (j == 6) html += '</td>'; } this.element.innerHTML = html; }, toggle: function() { this.element.classList.toggle('vert'); }, }; var today = new Date(), thisYear = today.getFullYear(), thisMonth = today.getMonth(); calendar.days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; selector = document.getElementById('month_select'); month_list = ['Январь', 'Февраль', 'Март', 'Апрель', ' Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']; for (let i = 0; i < month_list.length; i++){ selector.options[i] = new Option(month_list[i], i); } selector.selectedIndex = thisMonth; selector.addEventListener('', load); document.getElementById('year_input').addEventListener('input', load); calendar.element = document.querySelector('.month'); document.getElementById('month_rotate').addEventListener('click', function() { calendar.toggle(); }); function load(){ let year = document.getElementById('year_input').value; let month = selector.selectedIndex; calendar.update(year, month); calendar.render(); } document.addEventListener("DOMContentLoaded", ()=>{ document.getElementById('year_input').value = thisYear; load(); }); </script> </div> </body> </html> |
Блондинка,
мысли вслух: а зачем нужна была таблица? |
Цитата:
|
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>calendar-rotate</title> <style type="text/css"> #calendar_rotate { border: 1px solid hsl(0,0%,66%); padding: 5px; display: inline-block; width: 373px; } #navigation_panel { border: 1px solid hsl(0,0%,66%); padding: 5px 10px; margin: 0 0 5px 0; display: inline-block; white-space: nowrap; } #background_month { background-color: hsl(210,100%,85%); display: inline-block; } #month_select { background-color: transparent; color: hsl(210,100%,45%); font: 14px serif; border: 1px solid hsl(210,100%,45%); } table.month { border: 1px solid hsl(0,0%,66%); display flex; flex-flow column wrap; justify-content space-between; width: 367px; height: 367px; padding: 1px; table-layout: fixed; width: 100%; border-spacing: 2px; } table.vert { border: 1px solid #999; width: 367px; height: 367px; display flex; justify-content space-between; table-layout: fixed; width: 100%; border-spacing: 2px; } .month, .month.vert .week, td.day { display: table-cell; } .month.vert td.day { display: table-row; border: 1px solid hsl(0,0%,66%); } .month.vert td.day { display: table-row; border: 1px solid hsl(0,0%,66%); } td.day { border: 1px solid hsl(0,0%,66%); text-align: center; padding: 3.5px; background-color: hsl(210,100%,90%); color: hsl(210,100%,50%); } .day:nth-child(7n + 6), .day:nth-child(7n + 7) { background-color: hsl(348,100%,90%); color: hsl(348,100%,50%); } select { width: 109px; height: 29.5px; } input { border: 1px solid hsl(0,0%,66%); width: 54px; height: 25.5px;display: inline-block; text-align: center; } #month_minus, #year_minus { width: 28px; text-align: center; border-radius: 12px 0 0 12px / 10px 0 0 10px; margin: 0 -6px 0 0; position: relative; top: 1.5px; } #month_plus, #year_plus { width: 28px; text-align: center; border-radius: 0 12px 12px 0 / 0 10px 10px 0; margin: 0 0 0 -6px; position: relative; top: 1.5px; } #month_minus, #month_plus, #year_minus, #year_plus, #year_input { background-color: hsl(210,100%,85%); color: hsl(210,100%,45%); font: 14px serif; border: 1px solid hsl(210,100%,45%);} </style> </head> <body> <div id="calendar_rotate"> <div id="navigation_panel"> <button id="month_minus">◀</button> <span id="background_month"><select id="month_select"></select></span> <button id="month_plus">▶</button> <button id="year_minus">◀</button> <input id="year_input" type="number" size="4" value=""/> <button id="year_plus">▶</button> <button id="month_rotate">➙</button> </div> <table class="month vert"></table> <script> var calendar = { update: function(year, month) { this.days.length = 7; var stepDay = new Date(year, month, 1); stepDay.setDate(stepDay.getDate() + 0 - ((stepDay.getDay() + 6) % 7)); var lastDay = new Date(year, month + 1, 0); lastDay.setDate(lastDay.getDate() + 6 - ((lastDay.getDay() + 6) % 7)); while (stepDay <= lastDay) { this.days.push(stepDay.getDate()); stepDay.setHours(24); } }, render: function() { var html = ''; for (var i = 0, j = 0; i < this.days.length; j = ++i % 7) { if (j == 0) html += '<tr class="week">'; html += '<td class="day">' + this.days[i] + '</td>'; if (j == 6) html += '</td>'; } this.element.innerHTML = html; }, toggle: function() { this.element.classList.toggle('vert'); }, }; var today = new Date(), thisYear = today.getFullYear(), thisMonth = today.getMonth(); calendar.days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; selector = document.getElementById('month_select'); month_list = ['Январь', 'Февраль', 'Март', 'Апрель', ' Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']; for (let i = 0; i < month_list.length; i++){ selector.options[i] = new Option(month_list[i], i); } selector.selectedIndex = thisMonth; selector.addEventListener('', load); document.getElementById('year_input').addEventListener('input', load); calendar.element = document.querySelector('.month'); document.getElementById('month_rotate').addEventListener('click', function() { calendar.toggle(); }); function load(){ let year = document.getElementById('year_input').value; let month = selector.selectedIndex; calendar.update(year, month); calendar.render(); } document.addEventListener("DOMContentLoaded", ()=>{ document.getElementById('year_input').value = thisYear; load(); }); </script> </div> </body> </html> добавила расстояние между ячейками, ширину ячеек table-layout: fixed; width: 100%; border-spacing: 2px; |
Цитата:
|
Цитата:
|
Цитата:
1. Начать таки читать книжки по HTML, CSS и JavaScript 2. Бросить это дело |
Цитата:
Так ты и с таблицами не очень... В смысле секса. |
Цитата:
|
Цитата:
|
Блондинка, я женатый... :nono:
И не девачка чтобы чего-то фоткать и выкладывать. Да и с таблицами у меня нормально. Как, в прочем и с дивами... |
Цитата:
|
Блондинка,
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>calendar-rotate</title> <style type="text/css"> .month { --wh: 350px; /* менять размер тут px, em, и т.д.*/ border: 1px solid #000000; width: var(--wh); height: calc(var(--wh)/7 * var(--col)); border-spacing: 2px; margin: 4px; } .month td { border: 1px solid #000000; text-align: center; line-height: calc(var(--wh)/7 - 8px); } .month.vert { width: calc(var(--wh)/7 * var(--col)); } .month.vert tbody { display: grid; grid-gap: 2px; grid-template-columns: repeat(var(--col), 1fr); } .month.vert tbody tr { height: calc(var(--wh) - 4px); display: grid; grid-gap: 1px; grid-template-columns: repeat(1, 1fr); } .day { background-color: hsl(210, 100%, 90%); color: hsl(210, 100%, 50%); } .day:nth-child(7n + 6), .day:nth-child(7n + 7) { background-color: hsl(348, 100%, 90%); color: hsl(348, 100%, 50%); } </style> </head> <body> <div id="calendar_rotate"> <div id="navigation_panel"> <button id="month_minus">◀</button> <span id="background_month"><select id="month_select"></select></span> <button id="month_plus">▶</button> <button id="year_minus">◀</button> <input id="year_input" type="number" size="4" value="" /> <button id="year_plus">▶</button> <button id="month_rotate">➙</button> </div> <table class="month vert"></table> <script> var calendar = { update: function(year, month) { this.days.length = 7; var stepDay = new Date(year, month, 1); stepDay.setDate(stepDay.getDate() + 0 - ((stepDay.getDay() + 6) % 7)); var lastDay = new Date(year, month + 1, 0); lastDay.setDate(lastDay.getDate() + 6 - ((lastDay.getDay() + 6) % 7)); while (stepDay <= lastDay) { this.days.push(stepDay.getDate()); stepDay.setHours(24); } }, render: function() { var html = ''; for (var i = 0, j = 0; i < this.days.length; j = ++i % 7) { if (j == 0) html += '<tr class="week">'; html += '<td class="day">' + this.days[i] + '</td>'; if (j == 6) html += '</tr>'; } this.element.innerHTML = html; this.element.style.setProperty("--col", this.days.length / 7) }, toggle: function() { this.element.classList.toggle('vert'); }, }; var today = new Date(), thisYear = today.getFullYear(), thisMonth = today.getMonth(); calendar.days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; selector = document.getElementById('month_select'); month_list = ['Январь', 'Февраль', 'Март', 'Апрель', ' Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']; for (let i = 0; i < month_list.length; i++) { selector.options[i] = new Option(month_list[i], i); } selector.selectedIndex = thisMonth; selector.addEventListener('', load); document.getElementById('year_input').addEventListener('input', load); calendar.element = document.querySelector('.month'); document.getElementById('month_rotate').addEventListener('click', function() { calendar.toggle(); }); function load() { let year = document.getElementById('year_input').value; let month = selector.selectedIndex; calendar.update(year, month); calendar.render(); } document.addEventListener("DOMContentLoaded", () => { document.getElementById('year_input').value = thisYear; load(); }); </script> </div> </body> </html> |
рони,
этот календарь работает намного лучше, но всё равно пиксели прыгают, высота и ширина не одинаковые получаются при гориз и верт раскладке календаря... не проще ли взять эти два скрипта и объединить в один, и по нажатию на кнопку вывести одну таблицу а вторую скрыть? <script> Date.prototype.reduce = function(callback, value) { var year = this.getFullYear(); var month = this.getMonth(); var step = new Date(year, month, 1); var last = new Date(year, month + 1, 0); step.setHours(24 * (0 - ((step.getDay() + 6) % 7))); last.setHours(24 * (6 - ((last.getDay() + 6) % 7))); for (var i = 0; step <= last; i++) { value = callback(value, new Date(+step), i, this); step.setHours(24); } return value; }; var data = new Date(); var selectMonth = document.querySelector('#calendar_month'); var monthNames = [ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь',]; monthNames.forEach(function(monthName, i) { selectMonth.options[i] = new Option(monthName, i); }); selectMonth.addEventListener('change', function() { data.setMonth(this.value); createCalendar(data); }); var minusMonth = document.querySelector('.month_minus'); minusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() - 1); createCalendar(data); }); var plusMonth = document.querySelector('.month_plus'); plusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() + 1); createCalendar(data); }); var minusYear = document.querySelector('.year_minus'); minusYear.addEventListener('click', function() { data.setYear(data.getFullYear() - 1); createCalendar(data); }); var plusYear = document.querySelector('.year_plus'); plusYear.addEventListener('click', function() { data.setYear(data.getFullYear() + 1); createCalendar(data); }); var inputYear = document.querySelector('#calendar_year'); inputYear.addEventListener('input', function() { if (/^\d{4}$/.test(this.value)) { data.setYear(this.value); createCalendar(data); } }); var currentButton = document.querySelector('#presently'); currentButton.addEventListener('click', function() { data = new Date(); createCalendar(data); }); function createCalendar(data) { var now = new Date().setHours(0, 0, 0, 0); var year = data.getFullYear(); inputYear.value = year; var month = data.getMonth(); selectMonth.selectedIndex = month; currentButton.classList.remove('hide'); var indexcurDay; var cls = ['prevMonth', 'curMonth', 'nextMonth'], indexCls = 0; var html = data.reduce(function(value, current, index, source) { var date = current.getDate(); if (date == 1) indexCls++; var className = cls[indexCls]; if (+now == +current && indexCls == 1) { className += ' curDay'; currentButton.classList.add('hide'); indexcurDay = index % 7; } value[index % 7] += '<td class="' + className + '">' + date; return value }, ['<tr><td class="week-day">Пн.', '<tr><td class="week-day">Вт.', '<tr><td class="week-day">Ср.', '<tr><td class="week-day">Чт.', '<tr><td class="week-day">Пт.', '<tr><td class="week-day">Сб.', '<tr><td class="week-day">Вс.']); document.querySelector('#table tbody').innerHTML = html.join(''); var daysTd = document.querySelectorAll('.week-day'); if(indexcurDay !== void 0) daysTd[indexcurDay].classList.add('curDay'); } createCalendar(data); var timer; function refresh() { window.clearTimeout(timer); var finish = new Date().setHours(24, 0, 0, 0); finish -= data; timer = window.setTimeout(function() { createCalendar(data); refresh(); }, finish); } refresh(); </script> <script> var calendar = { update: function(year, month) { this.days.length = 7; var stepDay = new Date(year, month, 1); stepDay.setDate(stepDay.getDate() + 0 - ((stepDay.getDay() + 6) % 7)); var lastDay = new Date(year, month + 1, 0); lastDay.setDate(lastDay.getDate() + 6 - ((lastDay.getDay() + 6) % 7)); while (stepDay <= lastDay) { this.days.push(stepDay.getDate()); stepDay.setHours(24); } }, render: function() { var html = ''; for (var i = 0, j = 0; i < this.days.length; j = ++i % 7) { if (j == 0) html += '<tr class="week">'; html += '<td class="day">' + this.days[i] + '</td>'; if (j == 6) html += '</td>'; } this.element.innerHTML = html; }, toggle: function() { this.element.classList.toggle('vert'); }, }; var today = new Date(), thisYear = today.getFullYear(), thisMonth = today.getMonth(); calendar.days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; selector = document.getElementById('month_select'); month_list = ['Январь', 'Февраль', 'Март', 'Апрель', ' Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']; for (let i = 0; i < month_list.length; i++){ selector.options[i] = new Option(month_list[i], i); } selector.selectedIndex = thisMonth; selector.addEventListener('', load); document.getElementById('year_input').addEventListener('input', load); calendar.element = document.querySelector('.month'); document.getElementById('month_rotate').addEventListener('click', function() { calendar.toggle(); }); function load(){ let year = document.getElementById('year_input').value; let month = selector.selectedIndex; calendar.update(year, month); calendar.render(); } document.addEventListener("DOMContentLoaded", ()=>{ document.getElementById('year_input').value = thisYear; load(); }); </script> |
Блондинка,
сами, сами... |
рони,
можешь хоть объяснить, как создать одну таблицу где ячейки с классом week_day в начале каждой строки, и вторую где дни недели идут в ряд, и как заполнить эти обе таблицы датами, но только не 42 дня, а обрезать дни пред и будущего месяца после вс... |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #table_year { table-layout: fixed; width: 100%; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; } .td_month { width: 260px; height: 270px; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; } #num_year, #month_name { background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); font: bold 18px serif; text-align: center; } #num_year { width: 790px; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; padding: 5px; margin: 0 0 5px 0; } #month { display: inline-block; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; padding: 5px; width: auto; } #month_name { border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; padding: 5px; margin: 0 0 5px 0; } .day { border: 1px solid hsl(0, 0%, 66%); width: auto; line-height: 30px; text-align: center; margin: 1px; background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); } .table_month_vert { table-layout: fixed; width: 100%; height: 220px; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; } .week_day { width: 20%; background-color: hsl(210, 100%, 50%); color: hsl(210, 100%, 95%); } .table_month_vert tr:nth-child(n + 6) td.day { background-color: hsl(340, 100%, 95%); color: hsl(340, 100%, 50%); } .table_month_vert tr:nth-child(1) th { text-align: center; } </style> </head> <body> <div id="num_year"></div> <div id="month"> <div id="month_name"></div> <table class="table_month_vert"></table><br> <table class="table_month_gor"></table> </div> <script> var calendar = { update: function(year, month) { this.year = year; this.month = [ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ][month]; var startDay = new Date(year, month, 1); var offsetDay = (startDay.getDay() || 7) - 1; startDay.setDate(startDay.getDate() - offsetDay); var lastDay = new Date(startDay); var days = new Date(year, month + 1, 0).getDate(); days = Math.ceil((days + offsetDay) / 7) * 7; lastDay.setDate(lastDay.getDate() + days); this.data = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; while (startDay < lastDay) { this.data.push(startDay.getDate()); startDay.setHours(24); } }, render: function() { var html = ['<tr class="week_day">', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>']; for (var i = 0; i < this.data.length; i++) { html[i % 7] += '<td class="day">' + this.data[i]; } this.numYear.textContent = this.year; this.monthName.textContent = this.month; this.element.innerHTML = html.join(''); }, }; var today = new Date(), thisYear = today.getFullYear(), thisMonth = today.getMonth(); calendar.days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; calendar.numYear = document.querySelector('#num_year'); calendar.monthName = document.querySelector('#month_name'); calendar.element = document.querySelector('.table_month_vert'); calendar.update(thisYear, thisMonth); calendar.render(); </script> </body> </html> как создать и заполнить вторую таблицу с классом class="table_month_gor"? |
Цитата:
calendar.element = document.querySelector('.table_month_gor'); //calendar.update(thisYear, thisMonth); если нужен другой месяц и год уточнить calendar.render(); |
Цитата:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #table_year { table-layout: fixed; width: 100%; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; } .td_month { width: 260px; height: 270px; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; } #num_year, #month_name { background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); font: bold 18px serif; text-align: center; } #num_year { width: 790px; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; padding: 5px; margin: 0 0 5px 0; } #month { display: inline-block; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; padding: 5px; width: auto; } #month_name { border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; padding: 5px; margin: 0 0 5px 0; } .day { border: 1px solid hsl(0, 0%, 66%); width: auto; line-height: 30px; text-align: center; margin: 1px; background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); } .table_month_vert { table-layout: fixed; width: 100%; height: 220px; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; } .week_day { width: 20%; background-color: hsl(210, 100%, 50%); color: hsl(210, 100%, 95%); } .table_month_vert tr:nth-child(n + 6) td.day { background-color: hsl(340, 100%, 95%); color: hsl(340, 100%, 50%); } .table_month_vert tr:nth-child(1) th { text-align: center; } </style> </head> <body> <div id="num_year"></div> <div id="month"> <div id="month_name"></div> <table class="table_month_vert"></table><br> <table class="table_month_gor"></table> </div> <script> var calendar = { update: function(year, month) { this.year = year; this.month = [ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ][month]; var startDay = new Date(year, month, 1); var offsetDay = (startDay.getDay() || 7) - 1; startDay.setDate(startDay.getDate() - offsetDay); var lastDay = new Date(startDay); var days = new Date(year, month + 1, 0).getDate(); days = Math.ceil((days + offsetDay) / 7) * 7; lastDay.setDate(lastDay.getDate() + days); this.data = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; while (startDay < lastDay) { this.data.push(startDay.getDate()); startDay.setHours(24); } }, render: function() { var html = ['<tr class="week_day">', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>']; for (var i = 0; i < this.data.length; i++) { html[i % 7] += '<td class="day">' + this.data[i]; } this.numYear.textContent = this.year; this.monthName.textContent = this.month; this.element.innerHTML = html.join(''); }, }; var today = new Date(), thisYear = today.getFullYear(), thisMonth = today.getMonth(); calendar.days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; calendar.numYear = document.querySelector('#num_year'); calendar.monthName = document.querySelector('#month_name'); calendar.element = document.querySelector('.table_month_vert'); calendar.update(thisYear, thisMonth); calendar.render(); calendar.element = document.querySelector('.table_month_gor'); calendar.update(thisYear, thisMonth); calendar.render(); </script> </body> </html> |
Цитата:
либо взять рендер от вариантов с flex или grid. |
Цитата:
|
Блондинка,
Цитата:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #table_year { table-layout: fixed; width: 100%; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; } .td_month { width: 260px; height: 270px; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; } #num_year, #month_name { background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); font: bold 18px serif; text-align: center; } #num_year { width: 790px; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; padding: 5px; margin: 0 0 5px 0; } #month { display: inline-block; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; padding: 5px; width: auto; } #month_name { border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; padding: 5px; margin: 0 0 5px 0; } .day { border: 1px solid hsl(0, 0%, 66%); width: auto; line-height: 30px; text-align: center; margin: 1px; background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); } .table_month_vert { table-layout: fixed; width: 100%; height: 220px; border: 1px solid hsl(0, 0%, 66%); border-radius: 4px; } .week_day { width: 20%; background-color: hsl(210, 100%, 50%); color: hsl(210, 100%, 95%); } .table_month_vert tr:nth-child(n + 6) td.day { background-color: hsl(340, 100%, 95%); color: hsl(340, 100%, 50%); } .table_month_vert tr:nth-child(1) th { text-align: center; } table.horizontal td { background-image: -webkit-linear-gradient(top left, #FF0000, #0060BF); background-image: linear-gradient(to bottom right, #FF0000, #0060BF); } </style> </head> <body> <div id="num_year"></div> <div id="month"> <div id="month_name"></div> <table class="table_month_vert"></table><br> <table class="table_month_gor"></table> </div> <script> var calendar = { update: function(year, month) { this.year = year; this.month = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ][month]; var startDay = new Date(year, month, 1); var offsetDay = (startDay.getDay() || 7) - 1; startDay.setDate(startDay.getDate() - offsetDay); var lastDay = new Date(startDay); var days = new Date(year, month + 1, 0).getDate(); days = Math.ceil((days + offsetDay) / 7) * 7; lastDay.setDate(lastDay.getDate() + days); this.data = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; while (startDay < lastDay) { this.data.push(startDay.getDate()); startDay.setHours(24); } }, render: function(horizontal) { var html = ''; if (horizontal) { this.element.classList.add("horizontal"); for (var i = 0, j = 0; i < this.data.length; j = ++i % 7) { if (j == 0) html += '<tr class="week">'; html += '<td class="day">' + this.data[i] + '</td>'; if (j == 6) html += '</td>'; } } else { html = ['<tr class="week_day">', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>']; for (var i = 0; i < this.data.length; i++) { html[i % 7] += '<td class="day">' + this.data[i]; } html = html.join('') } this.numYear.textContent = this.year; this.monthName.textContent = this.month; this.element.innerHTML = html; }, }; var today = new Date(), thisYear = today.getFullYear(), thisMonth = today.getMonth(); calendar.days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; calendar.numYear = document.querySelector('#num_year'); calendar.monthName = document.querySelector('#month_name'); calendar.element = document.querySelector('.table_month_vert'); calendar.update(thisYear, thisMonth); calendar.render(true); calendar.element = document.querySelector('.table_month_gor'); calendar.render(); </script> </body> </html> |
рони,
:thanks: будем пробовать разобраться :write: :-) |
почему скрипт не работает, если со страницы удалить id="num_year", id="month_name", class="table_month_vert"? как это можно исправить?
почему поле ввода пустое? почему при выборе другого месяца в селекте ничего не меняется? как сделать чтобы можно было посмотреть за другой месяц/год? <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #navigation_panel { white-space: nowrap; border: 1px solid hsl(0, 0%, 50%); border-radius: 6px; padding: 5px 15px; margin: 0 0 5px; } input { width: 56px; display: inline-block; text-align: center; border: 1px solid hsl(210, 100%, 50%); } .table_rotate { border: 1px solid hsl(210, 100%, 50%); border-radius: 12px 12px 0 12px; background-color: hsl(210, 100%, 90%); color: hsl(210, 100%, 50%); } select { border: 1px solid hsl(210, 100%, 50%); background-color: hsl(210, 100%, 90%); color: hsl(210, 100%, 50%); } .table_month_horiz, .table_month_vert { width: 250px; height: 250px; border: 1px solid hsl(0, 0%, 50%); border-radius: 6px; } td { border: 1px solid hsl(0, 0%, 50%); border-radius: 6px; text-align: center; font: 14px serif; } #num_year, #month_name { background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); font: bold 18px serif; text-align: center; } #num_year { width: 250px; border: 1px solid hsl(0, 0%, 50%); border-radius: 4px; padding: 5px; margin: 0 0 5px; } #month { display: inline-block; border: 1px solid hsl(0, 0%, 50%); border-radius: 8px; padding: 5px; width: 250px; } #month_name { border: 1px solid hsl(0, 0%, 50%); border-radius: 4px; padding: 5px; margin: 0 0 5px; } .table_month_vert, .table_month_horiz { table-layout: fixed; width: 100%; height: 250px; border-spacing: 3px; } .table_month_horiz tr td:nth-child(1) { width: 20%; background-color: hsl(210, 100%, 88%); color: hsl(210, 100%, 50%); font-weight: bold; } /* названия будних дней недели */ .table_month_horiz tr:nth-child(n+6) td:nth-child(1) { width: 20%; background-color: hsl(348, 100%, 88%); color: hsl(348, 100%, 50%); font-weight: bold; } /* названия выходных дней недели */ .table_month_horiz tr td:nth-child(n+2) { width: 20%; background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); } /* будниe */ .table_month_horiz tr:nth-child(n+6) td:nth-child(n+2) { width: 20%; background-color: hsl(348, 100%, 95%); color: hsl(348, 100%, 50%); } /* выходныe */ .table_month_vert tr:nth-child(1) td.day { background-color: hsl(210, 100%, 88%); color: hsl(210, 100%, 50%); font-weight: bold; } /* названия будних дней недели */ .table_month_vert tr:nth-child(1) td.day:nth-child(n+6) { background-color: hsl(348, 100%, 88%); color: hsl(348, 100%, 50%); font-weight: bold; } /* названия выходных дней недели */ .table_month_vert tr:nth-child(n+2) td.day { background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); } /* будниe */ .table_month_vert tr:nth-child(n+2) td.day:nth-child(n+6) { background-color: hsl(348, 100%, 95%); color: hsl(348, 100%, 50%); } /* выходныe */ </style> </head> <body> <div id="num_year"></div> <div id="month"> <div id="navigation_panel"> <select id="select_month"></select> <input class="year_input" type="number" value=""> <button class="table_rotate">➘</button> </div> <div id="month_name"></div> <table class="table_month_horiz"></table> <table class="table_month_vert"></table> </div> <script> var calendar = { update: function(year, month) { this.year = year; this.month = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ][month]; var startDay = new Date(year, month, 1); var offsetDay = (startDay.getDay() || 7) - 1; startDay.setDate(startDay.getDate() - offsetDay); var lastDay = new Date(startDay); var days = new Date(year, month + 1, 0).getDate(); days = Math.ceil((days + offsetDay) / 7) * 7; lastDay.setDate(lastDay.getDate() + days); this.data = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; while (startDay < lastDay) { this.data.push(startDay.getDate()); startDay.setHours(24); } }, render: function(horiz) { var html = ''; if (horiz) { this.element.classList.add("horiz"); for (var i = 0, j = 0; i < this.data.length; j = ++i % 7) { if (j == 0) html += '<tr class="week">'; html += '<td class="day">' + this.data[i] + '</td>'; if (j == 6) html += '</td>'; } } else { html = ['<tr class="week_day">', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>']; for (var i = 0; i < this.data.length; i++) { html[i % 7] += '<td class="day">' + this.data[i]; } html = html.join('') } this.numYear.textContent = this.year; this.monthName.textContent = this.month; this.element.innerHTML = html; }, }; var today = new Date(), thisYear = today.getFullYear(), thisMonth = today.getMonth(); calendar.days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; calendar.numYear = document.querySelector('#num_year'); calendar.monthName = document.querySelector('#month_name'); calendar.element = document.querySelector('.table_month_vert'); calendar.update(thisYear, thisMonth); calendar.render(true); calendar.element = document.querySelector('.table_month_horiz'); calendar.render(); //Запрашивается селектор по тегу select. Добавляются месяца. selector = document.querySelector('select'); month_list = ['Январь', 'Февраль', 'Март', 'Апрель', ' Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']; for (var i = 0; i < month_list.length; i++){ selector.options[i] = new Option(month_list[i], i); } selector.selectedIndex = thisMonth; //Это для того, чтобы менять календарь при изменении месяца и года selector.addEventListener('change', load); document.querySelector('.year_input').addEventListener('change', load); calendar.element = document.querySelector('.month'); document.getElementById('month_rotate').addEventListener('click', function() { calendar.toggle(); }); //Вынесена загрузка в отдельную функцию. function load(){ var year = document.getElementById('year_input').value; var month = selector.selectedIndex; calendar.update(year, month); calendar.render(); } //при зыгрузке страницы вызывается тот же обработчик document.addEventListener("DOMContentLoaded", ()=>{ document.querySelector('.year_input').value = thisYear; load(); }); </script> </body> </html> |
другими словами как сделать чтобы наличие или отсутствие элемента в html-коде не влияло на отображение других элементов?
|
Блондинка,
проверить есть элемент или нет, есть что-то делаем, нет ничего. let elem = document.querySelector('.класс'); if(elem) { //... } |
так?
let elem = document.querySelector('.table_month_vert'); if(elem) { calendar.element = document.querySelector('.table_month_vert'); } |
Блондинка,
да |
Цитата:
и что не так с полем ввода, почему не показывает текущий год и нету навигации, просмотра других месяцев? <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #navigation_panel { white-space: nowrap; border: 1px solid hsl(0, 0%, 50%); border-radius: 6px; padding: 5px 15px; margin: 0 0 5px; } input { width: 56px; display: inline-block; text-align: center; border: 1px solid hsl(210, 100%, 50%); } .table_rotate { border: 1px solid hsl(210, 100%, 50%); border-radius: 12px 12px 0 12px; background-color: hsl(210, 100%, 90%); color: hsl(210, 100%, 50%); } select { border: 1px solid hsl(210, 100%, 50%); background-color: hsl(210, 100%, 90%); color: hsl(210, 100%, 50%); } .table_month_horiz, .table_month_vert { width: 250px; height: 250px; border: 1px solid hsl(0, 0%, 50%); border-radius: 6px; } td { border: 1px solid hsl(0, 0%, 50%); border-radius: 6px; text-align: center; font: 14px serif; } #num_year, #month_name { background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); font: bold 18px serif; text-align: center; } #num_year { width: 250px; border: 1px solid hsl(0, 0%, 50%); border-radius: 4px; padding: 5px; margin: 0 0 5px; } #month { display: inline-block; border: 1px solid hsl(0, 0%, 50%); border-radius: 8px; padding: 5px; width: 250px; } #month_name { border: 1px solid hsl(0, 0%, 50%); border-radius: 4px; padding: 5px; margin: 0 0 5px; } .table_month_vert, .table_month_horiz { table-layout: fixed; width: 100%; height: 250px; border-spacing: 3px; } .table_month_horiz tr td:nth-child(1) { width: 20%; background-color: hsl(210, 100%, 88%); color: hsl(210, 100%, 50%); font-weight: bold; } /* названия будних дней недели */ .table_month_horiz tr:nth-child(n+6) td:nth-child(1) { width: 20%; background-color: hsl(348, 100%, 88%); color: hsl(348, 100%, 50%); font-weight: bold; } /* названия выходных дней недели */ .table_month_horiz tr td:nth-child(n+2) { width: 20%; background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); } /* будниe */ .table_month_horiz tr:nth-child(n+6) td:nth-child(n+2) { width: 20%; background-color: hsl(348, 100%, 95%); color: hsl(348, 100%, 50%); } /* выходныe */ .table_month_vert tr:nth-child(1) td.day { background-color: hsl(210, 100%, 88%); color: hsl(210, 100%, 50%); font-weight: bold; } /* названия будних дней недели */ .table_month_vert tr:nth-child(1) td.day:nth-child(n+6) { background-color: hsl(348, 100%, 88%); color: hsl(348, 100%, 50%); font-weight: bold; } /* названия выходных дней недели */ .table_month_vert tr:nth-child(n+2) td.day { background-color: hsl(210, 100%, 95%); color: hsl(210, 100%, 50%); } /* будниe */ .table_month_vert tr:nth-child(n+2) td.day:nth-child(n+6) { background-color: hsl(348, 100%, 95%); color: hsl(348, 100%, 50%); } /* выходныe */ </style> </head> <body> <div id="num_year"></div> <div id="month"> <div id="navigation_panel"> <select id="select_month"></select> <input class="year_input" type="number" value=""> <button class="table_rotate">➘</button> </div> <div id="month_name"></div> <table class="table_month_horiz"></table> </div> <script> var calendar = { update: function(year, month) { this.year = year; this.month = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ][month]; var startDay = new Date(year, month, 1); var offsetDay = (startDay.getDay() || 7) - 1; startDay.setDate(startDay.getDate() - offsetDay); var lastDay = new Date(startDay); var days = new Date(year, month + 1, 0).getDate(); days = Math.ceil((days + offsetDay) / 7) * 7; lastDay.setDate(lastDay.getDate() + days); this.data = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; while (startDay < lastDay) { this.data.push(startDay.getDate()); startDay.setHours(24); } }, render: function(horiz) { var html = ''; if (horiz) { this.element.classList.add("horiz"); for (var i = 0, j = 0; i < this.data.length; j = ++i % 7) { if (j == 0) html += '<tr class="week">'; html += '<td class="day">' + this.data[i] + '</td>'; if (j == 6) html += '</td>'; } } else { html = ['<tr class="week_day">', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>']; for (var i = 0; i < this.data.length; i++) { html[i % 7] += '<td class="day">' + this.data[i]; } html = html.join('') } this.numYear.textContent = this.year; this.monthName.textContent = this.month; this.element.innerHTML = html; }, }; var today = new Date(), thisYear = today.getFullYear(), thisMonth = today.getMonth(); calendar.days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']; calendar.numYear = document.querySelector('#num_year'); calendar.monthName = document.querySelector('#month_name'); let elem = document.querySelector('.table_month_vert'); if(elem) { calendar.element = document.querySelector('.table_month_vert'); }; calendar.update(thisYear, thisMonth); calendar.render(true); calendar.element = document.querySelector('.table_month_horiz'); calendar.render(); //Запрашивается селектор по тегу select. Добавляются месяца. selector = document.querySelector('select'); month_list = ['Январь', 'Февраль', 'Март', 'Апрель', ' Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']; for (var i = 0; i < month_list.length; i++){ selector.options[i] = new Option(month_list[i], i); } selector.selectedIndex = thisMonth; //Это для того, чтобы менять календарь при изменении месяца и года selector.addEventListener('change', load); document.querySelector('.year_input').addEventListener('change', load); calendar.element = document.querySelector('.month'); document.getElementById('month_rotate').addEventListener('click', function() { calendar.toggle(); }); //Вынесена загрузка в отдельную функцию. function load(){ var year = document.getElementById('year_input').value; var month = selector.selectedIndex; calendar.update(year, month); calendar.render(); } //при зыгрузке страницы вызывается тот же обработчик document.addEventListener("DOMContentLoaded", ()=>{ document.querySelector('.year_input').value = thisYear; load(); }); </script> </body> </html> |
и как поменять класс таблице при клике на кнопку?
|
Цитата:
|
Часовой пояс GMT +3, время: 09:05. |