Simple calculator using js

Source: Internet
Author: User

// Complete code of the simple calculator is implemented using js. You can refer to the following suggestions for the shortcomings:
<! 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> simple computer </title>
</Head>
<Body>
<Table width = "33%" height = "193" border = "1">
<Tr>
<Td colspan = "4"> <input type = "text" size = "50" name = "result" id = "result"> </td>
</Tr>
<Tr>
<Td width = "24%"> <input type = "button" value = "1" onclick = "getNum (1)"> </td>
<Td width = "23%"> <input type = "button" value = "2" onclick = "getNum (2)"> </td>
<Td width = "26%"> <input type = "button" value = "3" onclick = "getNum (3)"> </td>
<Td width = "27%"> <input type = "button" value = "+" onclick = "getNum ('+')"> </td>
</Tr>
<Tr>
<Td> <input type = "button" value = "4" onclick = "getNum (4)"> </td>
<Td> <input type = "button" value = "5" onclick = "getNum (5)"> </td>
<Td> <input type = "button" value = "6" onclick = "getNum (6)"> </td>
<Td> <input type = "button" value = "-" onclick = "getNum ('-')"> </td>
</Tr>
<Tr>
<Td> <input type = "button" value = "7" onclick = "getNum (7)"> </td>
<Td> <input type = "button" value = "8" onclick = "getNum (8)"> </td>
<Td> <input type = "button" value = "9" onclick = "getNum (9)"> </td>
<Td> <input type = "button" value = "*" onclick = "getNum ('*')"> </td>
</Tr>
<Tr>
<Td> <input type = "button" value = "0" onclick = "getNum (0)"> </td>
<Td> <input type = "button" value = "." onclick = "getNum ('.')"> </td>
<Td> <input type = "button" value = "=" onclick = "getResult ()"> </td>
<Td> <input type = "button" value = "/" onclick = "getNum ('/')"> </td>
</Tr>
</Table>
<Script type = "text/javascript" language = "javascript">
// Determine whether to clear
Var clearContent = false;
// Obtain a number
Function getNum (num ){
If (clearContent ){
// Clear a result if it already exists
Document. getElementById ("result"). value = "";
ClearContent = false;
}
// Obtain the result object
Var objresult = document. getElementById ("result ");
// Implement digital connection
Objresult. value + = num;
}
// Obtain the result object Value
Function getResult (){
Var objresult2 = document. getElementById ("result ");
// Assign values to the result
Objresult2.value = objresult2.value + "=" + eval (objresult2.value );
ClearContent = true;
}
</Script>
</Body>
</Html>

From xweina's column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.