為什么我的代碼第一次可以渲染本地的時間出來,當我點擊上個月和下個月的按鈕時?
我點擊按鈕后,在控制臺輸出了HTML是正確的,但是,在頁面的顯示還是停留在第一次渲染的界面,不知道怎么回事?其他代碼和老師的一模一樣,我把我的render發(fā)上來
datepicker.render?=?function(direction){ var?year,month; if(monthDate){ ?year?=?monthDate.year; ?month?=?monthDate.month;} if(direction?===?'prev')?month--; if(direction?===??'next')?month++; //?console.log(month); var?html?=?datepicker.buildUi(year,month); //?document.body.innerHTML?=?html; //?$wrapper?=?document.querySelector('ui-datapicker-wrapper') //?if(!$wrapper){ ??? $wrapper?=?document.createElement('div'); $wrapper.className?=?'ui-datapicker-wrapper'; //?} $wrapper.innerHTML?=?html;document.body.appendChild($wrapper); console.log(html); }
2017-09-08
chrome 里查看頁面元素,可以看到,沒點擊一次按鈕,就創(chuàng)建一個wrapper,但后來創(chuàng)建的在init創(chuàng)建的下層,因此看起來數(shù)據(jù)是更新了,但頁面展示卻還是init的情況。所以,樓上真的很厲害,在你的解答里,我才知道,判斷wrapper是否存在是非常重要的。當容器存在才時候,就只是更新內(nèi)部的HTML即可。
2017-08-29
寫成這樣就行了:
datePicker.render = function(direction) {
??var year, month;
??if (monthData) {
???year = monthData.year;
???month = monthData.month;
??}
??if (direction === 'prev') month--;
??if (direction === 'next') month++;
??var html = datePicker.buildUi(year, month);
??if (!$wrapper) {
???$wrapper = document.createElement('div');
???$wrapper.className = 'ui-datepicker-wrapper';
???document.body.appendChild($wrapper);
??}
??$wrapper.innerHTML = html;
?}