js怎么写计算平方的代码
js怎么写计算平方的代码教程如下:
输入一个数字
实现结果:计算平方根结果
js实现代码如下:
function csclear()
{
document.cs.a.value = '';
//document.cs.b.value = '';
document.cs.c.value = '';
var ss = document.getElementById('output');
ss.innerHTML = '';
//var ss2 = document.getElementById('output2');
//ss2.innerHTML = '';
document.cs.a.focus();
}
function isNum(args)
{
args = args.toString();
if (args.length == 0)
return false;
for (var i = 0; i<args.length; i++)
{
if ((args.substring(i,i+1) < "0" || args.substring(i, i+1) > "9") && args.substring(i, i+1) != "."&& args.substring(i, i+1) != "-")
{
return false;
}
}
return true;
}
function cscheck(i)
{
var a = document.cs.a.value;
//var b = document.cs.b.value;
//var c = document.cs.c.value;
//alert(c);
if (i == "1" && !isNum(a))
{
a = a.substring(0,a.length-1);
document.cs.a.value = a;
return;
}
/*
if (i == "2" && !isNum(b))
{
b = b.substring(0,b.length-1);
document.cs.b.value = b;
return;
}
*/
}
function calcs()
{
var a = document.cs.a.value;
if (a < 0)
{
alert(a + " is < 0, Error!");
return;
}
var res = Math.sqrt(a);
var ss = document.getElementById('output');
var str = ss.innerHTML;
if (str == "")
{
str = "<br><center><h4>缁撴灉锛�</h4><hr><font size=4><table class="countrytable" border=1px><tbody>\n";
}
else
{
str =str.replace("</TBODY></TABLE></FONT></CENTER>","</tbody></table></font></center>");
str = str.replace("<TBODY>","<tbody>");
str =str.replace("</tbody></table></font></center>","");
}
var str2 = "<tbody><tr><td width=110 height=30 align=right>√<span style=\"TEXT-DECORATION: overline\">" + a + "</span> = </td><td width=230 align=left> " + res + "</td></tr>\n";
str = str.replace("<tbody>",str2);
str += "</tbody></table></font></center>\n";
ss.innerHTML = str;
document.cs.c.value=res;
}
显示效果:平方根计算器
参考文章: http://www.theicstock.com/gonglve/1044.html