前提・実現したいこと
現在node.jsのexpressでウェブアプリケーションを作ろうとしています。他サイトから引っ張ってきたカレンダーを使って、サイトに使用したいのですが、このカレンダーはnode.jsで使うことを想定していないため、innerHTMLを使うとエラーが出ます
質問 1
そもそもnode.jsでHTMLを後からいじるという考え方が間違っていますか?(だからinnerHTMLやappendが使えない?)
質問2
このカレンダーはcalendar.jsでテーブルを作成しているのですが、node.jsでこのテーブルを作るにはどうすれば良いのでしょうか?
node.js初心者なため、初歩的な質問かもしれませんが、回答お願いします。
発生している問題・エラーメッセージ
Cannot set property 'innerHTML' of null
該当のソースコード
app.js
1const express = require('express'); 2const mysql = require('mysql') 3const jsdom = require('jsdom-global')() 4const calendar = require('./calendar.js') 5 6app.get('/calendar',(req,res)=>{ 7 res.render('calendar.ejs') 8})
calendar.js
1var today = new Date(); 2var currentMonth = today.getMonth(); 3var currentYear = today.getFullYear(); 4 5function generate_year_range(start, end) { 6 var years = ""; 7 for (var year = start; year <= end; year++) { 8 years += "<option value='" + year + "'>" + year + "</option>"; 9 } 10 return years; 11 12} 13 14var today = new Date(); 15var currentMonth = today.getMonth(); 16var currentYear = today.getFullYear(); 17var selectYear = document.getElementById("year"); 18var selectMonth = document.getElementById("month"); 19 20var createYear = generate_year_range(1970, 2200); 21 22document.getElementById("year").innerHTML = createYear; 23 24var calendar = document.getElementById("calendar"); 25var lang = calendar.getAttribute('data-lang'); 26 27var months = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]; 28var days = ["日", "月", "火", "水", "木", "金", "土"]; 29 30var dayHeader = "<tr>"; 31for (day in days) { 32dayHeader += "<th data-days='" + days[day] + "'>" + days[day] + "</th>"; 33} 34dayHeader += "</tr>"; 35 36document.getElementById("thead-month").innerHTML = dayHeader; 37 38monthAndYear = document.getElementById("monthAndYear"); 39showCalendar(currentMonth, currentYear); 40 41 42 43function showCalendar(month, year) { 44 45 var firstDay = ( new Date( year, month ) ).getDay(); 46 47 tbl = document.getElementById("calendar-body"); 48 49 tbl.innerHTML = ""; 50 51 monthAndYear.innerHTML = months[month] + " " + year; 52 selectYear.value = year; 53 selectMonth.value = month; 54 55 // creating all cells 56 var date = 1; 57 for ( var i = 0; i < 6; i++ ) { 58 var row = document.createElement("tr"); 59 60 for ( var j = 0; j < 7; j++ ) { 61 if ( i === 0 && j < firstDay ) { 62 cell = document.createElement( "td" ); 63 cellText = document.createTextNode(""); 64 cell.appendChild(cellText); 65 row.appendChild(cell); 66 } else if (date > daysInMonth(month, year)) { 67 break; 68 } else { 69 cell = document.createElement("td"); 70 cell.setAttribute("data-date", date); 71 cell.setAttribute("data-month", month + 1); 72 cell.setAttribute("data-year", year); 73 cell.setAttribute("data-month_name", months[month]); 74 cell.className = "date-picker"; 75 cell.innerHTML = "<span>" + date + "</span>"; 76 77 if ( date === today.getDate() && year === today.getFullYear() && month === today.getMonth() ) { 78 cell.className = "date-picker selected"; 79 } 80 row.appendChild(cell); 81 date++; 82 } 83 } 84 85 tbl.appendChild(row); 86 } 87 88} 89 90function daysInMonth(iMonth, iYear) { 91return 32 - new Date(iYear, iMonth, 32).getDate(); 92}
calendar.ejs
1<div class="container-calendar"> 2 <h4 id="monthAndYear"></h4> 3 <div class="button-container-calendar"> 4 <button id="previous">‹</button> 5 <button id="next" >›</button> 6 </div> 7 8 <table class="table-calendar" id="calendar" data-lang="ja"> 9 <thead id="thead-month"></thead> 10 <tbody id="calendar-body"></tbody> 11 </table> 12 13 <div id="calendar_footer" class="footer-container-calendar"> 14 <label for="month">日付指定:</label> 15 <select id="month"> 16 <option value=0>1月</option> 17 <option value=1>2月</option> 18 <option value=2>3月</option> 19 <option value=3>4月</option> 20 <option value=4>5月</option> 21 <option value=5>6月</option> 22 <option value=6>7月</option> 23 <option value=7>8月</option> 24 <option value=8>9月</option> 25 <option value=9>10月</option> 26 <option value=10>11月</option> 27 <option value=11>12月</option> 28 </select> 29 <select id="year"></select> 30 <div id="footer_btn"></div> 31 </div> 32</div>

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/19 08:47