<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<button id="btn">click</button>
<script type="text/javascript">
var btn = document.getElementById("btn");
alert(btn.toString());
</script>
</body>
</html>没问题啊
function show()
{
alert(1);
}
function show(num)
{
alert(3);
}都是调用后面那个函数,前面那个就没了
<input type="text" id="inp" />
<script>
var inp = document.getElementById('inp');
inp.toString = function(){
return this.tagName;
}
alert(inp.toString()); // INPUT
</script>
Date.prototype.toString = function() {
return this.getYear()+"-"+this.getMonth()+"-"+this.getDate();
}Date 对象的默认toString()输出是: [object Date]
覆写后toString()的输出是:2008-9-22
IE不知道怎么获取通用的DOM对象
<html>
<head>
<script>
window.onload=function(){
if(typeof(HTMLElement)!="undefined")
HTMLElement.prototype.toMyString=function(){//增加一个新方法
if((this.tagName=="INPUT"&&this.type=="button")||this.tagName=="BUTTON"){
return "重写Button的toString()";
}
return this.toString();
}
HTMLElement.prototype.toString=function(){//重写toString,不过不起作用
if((this.tagName=="INPUT"&&this.type=="button")||this.tagName=="BUTTON"){
return "重写Button的toString()";
}
return this.toString();
}
}</script>
</head>
<body>
<input type="button" value="btn1" onclick="alert(this.toString())"/>
<input type="button" value="btn2" onclick="alert(this.toString())"/>
<input type="text" onclick="alert(this.toMyString())"/>
<button onclick="alert(this.toMyString())">btn3</button>
</body>
</html>
HTMLInputElement.prototype.toString = function()
{
return "id(" + this.id + ")name(" + this.name + ")type("+ this.type + ")value("+ this.value+")";
}