我想用 javascript 创建一个日历:
我通过这个函数创建元素:
createElem : function (name, attrs){
var el = document.createElement(name);
for(var key in attrs) {
el.setAttribute(key, attrs[key]);
}
return el;
}
桌子 :
var tb=this.createElem("table", {"class":"p-calendar", id: "p-calendar"});
行和单元格:
for(var i=2; i<to; i++){
tb.insertRow(i);
for(var j=0; j < 7; j++){
tb.rows[i].insertCell(j);
.
.
所以在我的循环中:
tb.rows[i].cells[j].className="day"; // it's work
但 :
tb.rows[i].cells[j].onclick = function(){
tb.rows[i].cells[j].id = "today";
} // Unable to set property 'id' of undefined or null reference
- 为什么会报错?!
- 将函数绑定到由 javascript 创建的元素的最佳方法是什么?
提前致谢。