如下所示:function apListView(objID,strCaption,colHeads,arButtons)
{
this.appendTo = document.getElementById(objID);
this.appendTo.style.cssText="padding:3px;background-color:rgb(206,225,250);border:1px solid rgb(107,147,207);margin: 0 6px;";
this.table = this.createListView(strCaption,colHeads,arButtons);
}
apListView.prototype.createListView= function (strCaption,colHeads,arButtons)
{
//参数说明:colHeads 是一个array,每个item的格式是 名称:宽度 的字符串
//生成表格
_this=this;
var btnSA=document.createElement("input");//创建一个按钮,选择所有
btnSA.value="Select All";
btnSA.type="button";
btnSA.style.cssText="font-size:9pt;border:1px #dddddd solid;padding:3px 3px 1px 3px;height:20px;width:80px;margin-top:3px;";
btnSA.onclick=function()
{
_this.SelAll(); //调用SelAll方法
}
}
apListView.prototype.SelAll = function() //定义一方法
{
return;
}//new 一个类实例
var userm=new apListView('usermanagerContenter',"Member List",["UserName:60","Password:60","Privilige:48","CreateDate:100","Resume:100","Action:100"],["Add","Sel All"]);
userm.SelAll = function() //重定义SelAll,发现不能成功,没有出现提示消息,这是怎么回事?
{
alert("Select All");
}
{
this.appendTo = document.getElementById(objID);
this.appendTo.style.cssText="padding:3px;background-color:rgb(206,225,250);border:1px solid rgb(107,147,207);margin: 0 6px;";
this.table = this.createListView(strCaption,colHeads,arButtons);
}
apListView.prototype.createListView= function (strCaption,colHeads,arButtons)
{
//参数说明:colHeads 是一个array,每个item的格式是 名称:宽度 的字符串
//生成表格
_this=this;
var btnSA=document.createElement("input");//创建一个按钮,选择所有
btnSA.value="Select All";
btnSA.type="button";
btnSA.style.cssText="font-size:9pt;border:1px #dddddd solid;padding:3px 3px 1px 3px;height:20px;width:80px;margin-top:3px;";
btnSA.onclick=function()
{
_this.SelAll(); //调用SelAll方法
}
}
apListView.prototype.SelAll = function() //定义一方法
{
return;
}//new 一个类实例
var userm=new apListView('usermanagerContenter',"Member List",["UserName:60","Password:60","Privilige:48","CreateDate:100","Resume:100","Action:100"],["Add","Sel All"]);
userm.SelAll = function() //重定义SelAll,发现不能成功,没有出现提示消息,这是怎么回事?
{
alert("Select All");
}
在JS前面放一个<div id="usermanagerContenter">kkk</div> 就可以了。 userm.SelAll = function() //重定义SelAll
{
alert("Select All");
}
userm.SelAll(); //在这里调用啊
{
alert("Select All");
}仅仅是给userm这个实例添加了SelAll函数,只能通过userm这个对象调用。再new一个apListView时,不能调用这个方法,调用的是在prototype里定义的方法。
你在什么地方调用时发现出问题了?
有写返回值,也有append,只是我这里代码没有完全截取出来,请看:
apListView.prototype.createTable = function (strCaption,colHeads,arButtons)
{
//参数说明:colHeads 是一个array,每个item的格式是 名称:宽度 的字符串
//生成表格
_this=this;
var oTable=document.createElement("table");
this.appendTo.appendChild(oTable);
... ...
var ofoot=oTable.createTFoot();
ofoot.insertRow();
var ofcell=ofoot.rows[0].insertCell(); var btnSA=document.createElement("input");//创建一个按钮,选择所有
btnSA.value="Select All";
btnSA.type="button";
btnSA.style.cssText="font-size:9pt;border:1px #dddddd solid;padding:3px 3px 1px 3px;height:20px;width:80px;margin-top:3px;";
btnSA.onclick=function()
{
_this.SelAll();
}
ofcell.appendChild(btnSA);
return oTable;
我就是创建一个table,table里有一个button,希望用户点击button时能激发我重载的SelAll函数userm.SelAll = function() //重定义SelAll,发现不能成功,没有出现提示消息,这是怎么回事?
{
alert("Select All");
}但发现不能达到我的要求!
那么怎样做才能达到我的要求,SelAll方法可能在不同的环境有不同的处理方式?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title><script type="text/javascript">
function ArList() {
}
ArList.prototype.createList = function(containerId) {
var container = document.getElementById(containerId);
var tableTag = document.createElement("table");
try {
var trTag = tableTag.insertRow(0);
} catch(e) {
alert(e.message);
}
var tdTag = document.createElement("td");
var buttonInput = document.createElement("input");
var targetThis = this;
buttonInput.type = "button";
buttonInput.value = "test";
buttonInput.onclick = function() {
targetThis.selectAll();
};
tdTag.appendChild(buttonInput);
trTag.appendChild(tdTag);
container.appendChild(tableTag);
};
ArList.prototype.selectAll = function() {
return;
}
window.onload = function() {
var arList = new ArList();
arList.createList("container");
arList.selectAll = function() {
alert("Select All");
};
};
</script></head>
<body>
<div id="container"></div>
</body>
</html>
改成
var _this = this;
如果不加var,则第二个对象会覆盖第一个