为什么会出错,Uncaught ReferenceError: text is not defined ,求路过的大神指导

家有小辉 2014-05-01 05:43:19
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>计算器</title>

</head>
<body>
<script type="text/javascript">
var isnnum=false;
var n1=0;
var fu="";
var zong=0;
var isdian=false;
function Backspace ()
{
var text_ing =text.value;
if(text_ing!=0)
{
var text_str=String(Math.abs(text_ing));
if (text_ing==1||text_str==1)
{
text.value="0";
isdian=false;
}
}
else
{
text.value=text_ing.substr(0,text_ing.length-1);
}
}
function zf()
{
var str=parseFloat(text.value);
if (str>0)
{
text.value="-"+str;
}
if (str<0)
{
text.value=Math.abs(str);
}
}

function num(n)
{
if (isnnum==true)
{
if (text.value=="0.")
{
text.value=text.value+n;
}
else
{
text.value=n;
}
isnnum=false;
}
else
{
if (text.value=="0")
{
text.value=n;
}
else
{
text.value=text.value+n;
}
}
n1=parseFloat(text.value);
}
function dian()
{
var isfirst=isfloor();
if(isnnum==true||text.value=="0")
{
text.value="0.";
}
if (isnnum==false&&isfirst==true)
{
text.value="0.";
}
else if (isdian==false)
{
if (parseFloat(text.value)=="0")
{
text.value="0.";
}
else if (isfirst==true)
{
text.value=text.value;
}
else
{
text.value=text.value+".";
}
isdian=true;
}
}
function isfloor()
{
var int1=parseFloat(text.value);
var int2=Math.floor(int1);
if (int1>int2)
{
return true;
}
else
{
return false;
}
}
function qingkong()
{
isdian=isnnum=false;
zong=parseFloat(text.value);
text.value="0";
isnnum=true;
fu=base="";
dd=jsq=zong=n1=0;
}
function isXiao(n)
{
var int1=parseFloat(text.value);
var int2=Math.floor(int1);
if (int1>int2)
{
return true;
}
else
{
return false;
}
}
function js(s)
{
var lin=parseFloat(n1);
if ((fu=="=")&&(s=="="))
{
if (isXiao(zong)==true)
{
text.value=zong.toFixed(4);
}
else
{
text.value=zong;
}
fu=s;
}
else
{
if ("+"==fu)
{zong+=lin;}
else if ("-"==fu)
{
zong-=lin;
}
else if ("/"==fu)
{
zong/=lin;
}
else if ("*"==fu)
{
zong*=lin;
}
else
{
zong=lin;
}
if (isXiao(zong)==true)
{
text.value=zong.toFixed(4);
}
else
{
text.value=zong;
}
fu=s;
}
isnnum=true;
}
</script>

<table width="213" height="214" border="1" cellspacing="5" style=" background-color:#CCFFFF">
<tr>
<td id="td1" colspan="6" height="20">
<input type="text" name="text" value="0" size="33" style="background-color:#FFFFFF;font-size:9pt;color:#333333;border:1px solid #999999; cursor:text">
</td>
</tr>
<tr>
<td align="right" height="30" colspan="2">
<div align="left">
<input type="button" name="back" value="Backspace" onclick="Backspace() " >
</div>
</td>
<td align="right">
<div align="left">
<input align="right" type="button" name="C" value="C" onclick="qingkong()">
</div>
</td>
<td>
<input type="button" name="Button" value=" = " onclick="js("=")">
</td>
</tr>
<tr>
<td width="47" height="28">
<input type="button" name="A7" value=" 7 " onclick="num(7)">
</td>
<td width="50">
<input type="button" name="A8" value=" 8 " onclick="num(8)">
</td>
<td width="47">
<input type="button" name="A9" value=" 9 " onclick="num(9)">
</td>
<td width="42">
<input type="button" name="chu2" value=" / " onclick="js("/")">
</td>
</tr>
<tr>
<td width="47" height="28">
<input type="button" name="A4" value=" 4 " onclick="num(4)">
</td>
<td width="50">
<input type="button" name="A5" value=" 5 " onclick="num(5)">
</td>
<td width="47">
<input type="button" name="A6" value=" 6 " onclick="num(6)">
</td>
<td width="42">
<input type="button" name="cheng" value=" * " onclick="js(*)">
</td>
</tr>
<tr>
<td width="47" height="28">
<input type="button" name="A1" value=" 1 " onclick="num(1)">
</td>
<td width="50">
<input type="button" name="A2" value=" 2 " onclick="num(2)">
</td>
<td width="47">
<input type="button" name="A3" value=" 3 " onclick="num(3)">
</td>
<td width="42">
<input type="button" name="jian" value=" - " onclick="js("-")">
</td>
</tr>
<tr>
<td width="47" height="28">
<input type="button" name="A0" value="0" onclick="num(0)">
</td>
<td width="50">
<input type="button" name="jgj" value="+/-" onclick="zf()">
</td>
<td width="47">
<input type="button" name="dian" value=" . " onclick="dian()">
</td>
<td width="42">
<input type="button" name="jia" value=" + " onclick="js("+")">
</td>
</tr>
</table>
</body>
</html>
...全文
5190 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jyhkss 2014-12-13
  • 打赏
  • 举报
回复
数据是字符串得转义成字符串
家有小辉 2014-05-03
  • 打赏
  • 举报
回复
引用 7 楼 net_lover 的回复:
ie可以直接写 text.value 非IE不支持
原来是这样子,谢谢
googny 2014-05-02
  • 打赏
  • 举报
回复
敢情这是来让调代码的啊。 按照楼上的 解决后 你的text undefined的问题解决的,但是还是有各种各样的问题, 楼主加油。
家有小辉 2014-05-02
  • 打赏
  • 举报
回复
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>计算器</title> </head> <body> <script type="text/javascript"> var isnnum=false; var n1=0; var fu=""; var zong=0; var isdian=false; function Backspace() { var text_ing =text.value; if(text_ing!=0) { var text_str=String(Math.abs(text_ing)); if (text_ing.length==1||text_str.length==1) { text.value="0"; isdian=false; } else { text.value=text_ing.substring(0,text_ing.length-1); } } } function zf() { var str=parseFloat(text.value); if (str>0) { text.value="-"+str; } if (str<0) { text.value=Math.abs(str); } } function num(n) { if (isnnum==true) { if (text.value=="0.") { text.value=text.value+n; } else { text.value=n; } isnnum=false; } else { if (text.value=="0") { text.value=n; } else { text.value=text.value+n; } } n1=parseFloat(text.value); } function dian() { var isfirst=isfloor(); if(isnnum==true||text.value=="0") { text.value="0."; } if (isnnum==false&&isfirst==true) { text.value="0."; } else if (isdian==false) { if (parseFloat(text.value)=="0") { text.value="0."; } else if (isfirst==true) { text.value=text.value; } else { text.value=text.value+"."; } isdian=true; } } function isfloor() { var int1=parseFloat(text.value); var int2=Math.floor(int1); if (int1>int2) { return true; } else { return false; } } function qingkong() { isdian=isnnum=false; zong=parseFloat(text.value); text.value="0"; isnnum=true; fu=base=""; dd=jsq=zong=n1=0; } function isXiao(n) { var int1=parseFloat(text.value); var int2=Math.floor(int1); if (int1>int2) { return true; } else { return false; } } function js(s) { var lin= parseFloat(n1); if ((fu=="=")&&(s=="=")) { if (isXiao(zong)==true) { text.value=zong.toFixed(4); } else { text.value=zong; } } else if((fu=="=")&&(s==("*")||s==("+")||s==("-")||s==("/"))) { if (isXiao(zong)==true) { text.value=zong.toFixed(4); } else { text.value=zong; } fu=s; } else { if ("+"==fu) {zong+=lin;} else if ("-"==fu) { zong-=lin; } else if ("/"==fu) { zong/=lin; } else if ("*"==fu) { zong*=lin; } else { zong=lin; } if (isXiao(zong)==true) { text.value=zong.toFixed(4); } else { text.value=zong; } fu=s; } isnnum=true; } </script> <table width="213" height="214" border="1" cellspacing="5" style=" background-color:#CCFFFF"> <tr> <td id="td1" colspan="6" height="20"> <input type="text" name="text" value="0" size="33" style="background-color:#FFFFFF;font-size:9pt;color:#333333;border:1px solid #999999; cursor:text"> </td> </tr> <tr> <td align="right" height="30" colspan="2"> <div align="left"> <input type="button" name="back" value="Backspace" onclick="Backspace()" > </div> </td> <td align="right"> <div align="left"> <input align="right" type="button" name="C" value="C" onclick="qingkong()"> </div> </td> <td> <input type="button" name="Button" value=" = " onclick="js('=')"> </td> </tr> <tr> <td width="47" height="28"> <input type="button" name="A7" value=" 7 " onclick="num(7)"> </td> <td width="50"> <input type="button" name="A8" value=" 8 " onclick="num(8)"> </td> <td width="47"> <input type="button" name="A9" value=" 9 " onclick="num(9)"> </td> <td width="42"> <input type="button" name="chu2" value=" / " onclick="js('/')"> </td> </tr> <tr> <td width="47" height="28"> <input type="button" name="A4" value=" 4 " onclick="num(4)"> </td> <td width="50"> <input type="button" name="A5" value=" 5 " onclick="num(5)"> </td> <td width="47"> <input type="button" name="A6" value=" 6 " onclick="num(6)"> </td> <td width="42"> <input type="button" name="cheng" value=" * " onclick="js('*')"> </td> </tr> <tr> <td width="47" height="28"> <input type="button" name="A1" value=" 1 " onclick="num(1)"> </td> <td width="50"> <input type="button" name="A2" value=" 2 " onclick="num(2)"> </td> <td width="47"> <input type="button" name="A3" value=" 3 " onclick="num(3)"> </td> <td width="42"> <input type="button" name="jian" value=" - " onclick="js('-')"> </td> </tr> <tr> <td width="47" height="28"> <input type="button" name="A0" value="0" onclick="num(0)"> </td> <td width="50"> <input type="button" name="jgj" value="+/-" onclick="zf()"> </td> <td width="47"> <input type="button" name="dian" value=" . " onclick="dian()"> </td> <td width="42"> <input type="button" name="jia" value=" + " onclick="js('+')"> </td> </tr> </table> </body> </html> 在IE调试没有出错,在Google Chrome就显示text没有定义。。。。。。。。。。。。
孟子E章 2014-05-02
  • 打赏
  • 举报
回复
ie可以直接写 text.value 非IE不支持
家有小辉 2014-05-02
  • 打赏
  • 举报
回复
引用 3 楼 tonghaoqi__ 的回复:
敢情这是来让调代码的啊。 按照楼上的 解决后 你的text undefined的问题解决的,但是还是有各种各样的问题, 楼主加油。
正在努力中
家有小辉 2014-05-02
  • 打赏
  • 举报
回复
引用 2 楼 net_lover 的回复:
name="text" 改成 id="text" 代码中使用document.getElementById("text")得到这个对象。而不是简单地text.value等
我发现他在IE可以用的只是在Google Chrome就不可以了,受教了
孟子E章 2014-05-01
  • 打赏
  • 举报
回复
name="text" 改成 id="text" 代码中使用document.getElementById("text")得到这个对象。而不是简单地text.value等
ThinkingFatCat 2014-05-01
  • 打赏
  • 举报
回复
你确实没定于text阿?

87,921

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧