如何使我的JS / HTML5计算器结果多线化?基本上,如果结果不适合第一行,它会在下一行继续结果。我无法在StackOverFlow,YouTube或任何地方找到如何在任何地方执行此操作。
我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Calc</title>
<style>
button {width: 30px; height: 30px;}
input {text-align: center;height: 30px;}
.calc {border: groove 6px; margin-left: 530px; margin-right: 530px; padding-top: 10px; padding-bottom: 10px; height: 255px;}
.results {padding-bottom: 7px;}
.top {float: left; padding-left: 20px;}
.numbers {float: left; padding-left: 20px; padding-top: 15px;}
.symbols {float: right; margin-top: -30px; padding-right: 20px;}
</style>
<script>
function myFunction(clickedId) {
document.calc.result.value+=clickedId;
}
function Clear() {
document.calc.result.value="";
}
function compute() {
try{
var inp=eval(document.calc.result.value);
document.calc.result.value=inp;
}
catch(err){
document.calc.result.value="error";
}
}
function doMath() {
var inputNum1=document.calc.result.value;
var result = Math.sqrt(inputNum1);
document.calc.result.value = result;
}
function myMultiply() {
var x = parseInt (document.calc.result.value, 10);
var y = x*x;
alert(x + " times " + x + " equals " + y);
return false;
}
function compute() {
try{
var inp=eval(document.calc.result.value);
if(document.calc.result.value==inp)
inp=inp*inp
document.calc.result.value=inp;
}
catch(err){
document.calc.result.value="error";
}
}
</script>
</head>
<body>
<div class="calc">
<center>
<div class="results">
<form name="calc">
<input type="text" name="result" readonly>
</form>
</div>
<table>
<div class="top">
<button type="button" id="CLEAR" onclick="Clear()"><font color="#CC0000"><b>c</b></font></button> <!--Izdzēst rakstīto-->
<button type="button" id="3.141592653589793" onclick="myFunction(this.id)">π</button> <!--Skaitlis 3.14...-->
<button type="button" id="6.283185307179586" onclick="myFunction(this.id)">τ</button><br> <!--Skaitlis 6.28...-->
</div>
<div class="numbers">
<button type="button" id="1" onclick="myFunction(this.id)">1</button> <!--Skaitlis 1-->
<button type="button" id="2" onclick="myFunction(this.id)">2</button> <!--Skaitlis 2-->
<button type="button" id="3" onclick="myFunction(this.id)">3</button><br> <!--Skaitlis 3-->
<button type="button" id="4" onclick="myFunction(this.id)">4</button> <!--Skaitlis 4-->
<button type="button" id="5" onclick="myFunction(this.id)">5</button> <!--Skaitlis 5-->
<button type="button" id="6" onclick="myFunction(this.id)">6</button><br> <!--Skaitlis 6-->
<button type="button" id="7" onclick="myFunction(this.id)">7</button> <!--Skaitlis 7-->
<button type="button" id="8" onclick="myFunction(this.id)">8</button> <!--Skaitlis 8-->
<button type="button" id="9" onclick="myFunction(this.id)">9</button><br> <!--Skaitlis 9-->
<button type="button" id="0" onclick="myFunction(this.id)">0</button><br> <!--Skaitlis 0-->
</div>
<div class="symbols">
<button type="button" id="ANS" onclick="compute()">=</button><br> <!--Vienādības zīme-->
<button type="button" id="+" onclick="myFunction(this.id)">+</button><br> <!--Plusa zīme-->
<button type="button" id="-" onclick="myFunction(this.id)">-</button><br> <!--Mīnusa zīme-->
<button type="button" id="*" onclick="myFunction(this.id)">x</button><br> <!--Reizināšanas zīme-->
<button type="button" id="/" onclick="myFunction(this.id)">÷</button><br> <!--Dalīšanas zīme-->
<button type="button" id="SQRT" onclick="doMath()">√</button><br> <!--Kvadrātsakne-->
<button type="button" id="imp*inp" onclick="compute()">²</button><br> <!--Kvadrāts-->
</div>
</table>
</center>
</div>
<center><p>Thanks to my peeps at <a href="http://www.stackoverflow.com">StackOverFlow</a> for helping me with some issues!</p></center> <!--Pateicības piezīme-->
</body>
</html>
&#13;
答案 0 :(得分:1)
只需使用textarea
元素而不是input type="text"
:
function myFunction(clickedId) {
document.calc.result.value+=clickedId;
}
function Clear() {
document.calc.result.value="";
}
function compute() {
try{
var inp=eval(document.calc.result.value);
document.calc.result.value=inp;
}
catch(err){
document.calc.result.value="error";
}
}
function doMath() {
var inputNum1=document.calc.result.value;
var result = Math.sqrt(inputNum1);
document.calc.result.value = result;
}
function myMultiply() {
var x = parseInt (document.calc.result.value, 10);
var y = x*x;
alert(x + " times " + x + " equals " + y);
return false;
}
function compute() {
try{
var inp=eval(document.calc.result.value);
if(document.calc.result.value==inp)
inp=inp*inp
document.calc.result.value=inp;
}
catch(err){
document.calc.result.value="error";
}
}
button {width: 30px; height: 30px;}
textarea {text-align: center;height: 30px;width: 60px}
.calc {border: groove 6px; margin-left: 530px; margin-right: 530px; padding-top: 10px; padding-bottom: 10px; height: 255px;}
.results {padding-bottom: 7px;}
.top {float: left; padding-left: 20px;}
.numbers {float: left; padding-left: 20px; padding-top: 15px;}
.symbols {float: right; margin-top: -30px; padding-right: 20px;}
<!DOCTYPE html>
<html>
<head>
<title>Calc</title>
</head>
<body>
<div class="calc">
<center>
<div class="results">
<form name="calc">
<textarea name="result" readonly></textarea>
</form>
</div>
<table>
<div class="top">
<button type="button" id="CLEAR" onclick="Clear()"><font color="#CC0000"><b>c</b></font></button> <!--Izdzēst rakstīto-->
<button type="button" id="3.141592653589793" onclick="myFunction(this.id)">π</button> <!--Skaitlis 3.14...-->
<button type="button" id="6.283185307179586" onclick="myFunction(this.id)">τ</button><br> <!--Skaitlis 6.28...-->
</div>
<div class="numbers">
<button type="button" id="1" onclick="myFunction(this.id)">1</button> <!--Skaitlis 1-->
<button type="button" id="2" onclick="myFunction(this.id)">2</button> <!--Skaitlis 2-->
<button type="button" id="3" onclick="myFunction(this.id)">3</button><br> <!--Skaitlis 3-->
<button type="button" id="4" onclick="myFunction(this.id)">4</button> <!--Skaitlis 4-->
<button type="button" id="5" onclick="myFunction(this.id)">5</button> <!--Skaitlis 5-->
<button type="button" id="6" onclick="myFunction(this.id)">6</button><br> <!--Skaitlis 6-->
<button type="button" id="7" onclick="myFunction(this.id)">7</button> <!--Skaitlis 7-->
<button type="button" id="8" onclick="myFunction(this.id)">8</button> <!--Skaitlis 8-->
<button type="button" id="9" onclick="myFunction(this.id)">9</button><br> <!--Skaitlis 9-->
<button type="button" id="0" onclick="myFunction(this.id)">0</button><br> <!--Skaitlis 0-->
</div>
<div class="symbols">
<button type="button" id="ANS" onclick="compute()">=</button><br> <!--Vienādības zīme-->
<button type="button" id="+" onclick="myFunction(this.id)">+</button><br> <!--Plusa zīme-->
<button type="button" id="-" onclick="myFunction(this.id)">-</button><br> <!--Mīnusa zīme-->
<button type="button" id="*" onclick="myFunction(this.id)">x</button><br> <!--Reizināšanas zīme-->
<button type="button" id="/" onclick="myFunction(this.id)">÷</button><br> <!--Dalīšanas zīme-->
<button type="button" id="SQRT" onclick="doMath()">√</button><br> <!--Kvadrātsakne-->
<button type="button" id="imp*inp" onclick="compute()">²</button><br> <!--Kvadrāts-->
</div>
</table>
</center>
</div>
<center><p>Thanks to my peeps at <a href="http://www.stackoverflow.com">StackOverFlow</a> for helping me with some issues!</p></center> <!--Pateicības piezīme-->
</body>
</html>