js面向对象编程遇到个问题,求解惑。。。
this.editUser = function (id) {
var user = this.getUserById(id);
var html = '<td><input type="checkbox" name="selectBox" value="'+user['id']+'" onchange="javascript:this.onChangeSelect('+user['id']+');"></td>';
html += '<td><input type="text" id="name_'+user['id']+'" name="name" value="'+user['name']+'"></td>';
html += '<td><input type="text" id="birthday_'+user['id']+'" name="birthday" value="'+user['birthday']+'"></td>'
html += '<td><input type="radio" value="M" name="gender_' +user['id']+'"';
if(user['gender'] == 'M') html += ' checked="checked"';
html += '> 男 <input type="radio" name="gender_'+user['id']+'" value="F"';
if(user['gender'] == 'F') html += ' checked="checked"';
html += '>女</td>';
html += '<input type="button" value="保存" onclick="javascript:
this.saveUser('+id+');"> <input type="button" value="取消" onclick="javascript:
this.cancelEditUser('+id+')"></td>';
document.getElementById('row_'+id).innerHTML = html;
}
html只认识window的对象,我要怎么改动才可以实现在html中调用这个对象中的方法呢???
------解决方案--------------------this.editUser = function (id) {
在上面这句之前定义 var window.aaaaa=this;
然后在onclick里面把this换成 aaaaa
------解决方案--------------------你那样用this对象为button对象。。创建DOM的方法来添加,不能通过设置innerHTML,要不你就需要将你的实例变量保存为全局的,然后通过实例变量来调用,这样兼容性很差
this.editUser = function (id) {
var user = this.getUserById(id);
/////////// /////////// /////////// /////////// ///////////
var me = this; ///////////
var tr = document.getElementById('row_' + id), td = tr.insertCell(0);
var input = document.createElement('input');
input.type = "checkbox";
input.name = "selectBox"
name.value = user['id']
input.onchange = function () { me.onChangeSelect(user['id']); }
var html = '<td><input type="checkbox" name="selectBox" value="' + user['id'] + '" onchange="javascript:this.onChangeSelect(' + user['id'] + ');"></td>';
/////////// /////////// /////////// ///////////&