2015-08-21 6 views
0

Как сделать результаты моего калькулятора JS/HTML5 многострочными? В принципе, если результат не соответствует первой строке, он продолжает результат в следующей строке. Я не мог найти, как это сделать, ни на StackOverFlow, ни на YouTube, ни где-либо.Как сделать результаты моего калькулятора JS/HTML5 многострочными?

Мой код:

<!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--> 
 
\t <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--> 
 
\t <button type="button" id="SQRT" onclick="doMath()">√</button><br> <!--Kvadrātsakne--> 
 
\t <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>

+2

Вы не можете обернуть те xt в '', вместо этого используйте 'textarea' или какой-то другой элемент уровня блока. – Teemu

ответ

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--> 
 
\t <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--> 
 
\t <button type="button" id="SQRT" onclick="doMath()">√</button><br> <!--Kvadrātsakne--> 
 
\t <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>

+0

Как изменить размер? – svens

+0

Вы можете использовать CSS для изменения размера 'textarea'. Если вы хотите изменить его в JavaScript, используйте 'document.calc.result.style.width' и' document.calc.result.style.height' для установки измерений. –

+0

Что такое точный код? textarea {height: 30px; width: 60px;} не работает. – svens