在我的框架中使用 Ruby on Rails。我已經(jīng)編寫了代碼來創(chuàng)建棋盤。當我第一次導航到包含附加元素的 div 頁面時,我由 JavaScript 創(chuàng)建的 HTML 元素消失了。棋盤在一瞬間可見,然后在第一次導航到頁面時消失,但刷新會使棋盤出現(xiàn)并停留。這是我的 JavaScript 文件: $(function() { var $boardContainer = document.querySelector('.board-container'); //grabs the div added to the index page -JB var $table = document.createElement("table"); function buildBoard() { for (var i = 0; i < 8; i++) { var $tr = document.createElement('tr'); for (var n = 0; n < 8; n++) { var $td = document.createElement('td'); $td.setAttribute('data-coord', i+','+n) //grabs the row iterator (i) and the cell iterator (n) and uses them for the coords! -JB if (i%2 == n%2) { //every even square is white, odd is black -JB $td.className = "white"; } else { $td.className = "black"; } $tr.appendChild($td); } $table.appendChild($tr); } $boardContainer.appendChild($table); } buildBoard(); });這是元素附加到的元素:<%= link_to 'Home', root_path, class: 'btn btn-primary'%><div class="board-container d-flex justify-content-center"></div>想弄清楚為什么棋盤在初始頁面加載時不會保持呈現(xiàn)狀態(tài),但會在刷新時保持狀態(tài)。
導航到頁面時 HTML 元素消失,頁面刷新時重新出現(xiàn)
拉風的咖菲貓
2021-12-23 10:36:48