【问题标题】:How do I make all JavaScript buttons the same size?如何使所有 JavaScript 按钮的大小相同?
【发布时间】:2015-11-15 09:13:41
【问题描述】:

如何使所有 JavaScript 按钮的大小相同?我正在制作一个计算器,如果所有按钮的大小不同,它看起来很恶心。我已经尝试执行此操作至少一个小时,但找不到任何解释如何执行此操作的 StakOverFlow 帖子或教程。

我的代码:

<!DOCTYPE html>
<html>
<head>
<title>Calc</title>
<style>
.calc {border: groove 6px; margin-left: 530px; margin-right: 530px; padding-top: 10px; padding-bottom: 10px; height: 200px;}

input {text-align: center;height: 30px;}

.results {padding-bottom: 7px;}

.top {float: left; padding-left: 20px;}

.numbers {float: left; padding-left: 20px; padding-top: 15px;}

.symbols {float: right; margin-top: -40px; padding-right: 15px;}
</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">C</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> <!--Skaitlis 6.28...-->
</div>
<br>
<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> <!--Skaitlis 3-->
<br>
    <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> <!--Skaitlis 6-->
<br>
    <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> <!--Skaitlis 9-->
<br>
    <button type="button" id="0" onclick="myFunction(this.id)">0</button> <!--Skaitlis 0-->
</div>
<br>
<div class="symbols">
    <button type="button" id="ANS" onclick="compute()">=</button> <!--Vienādības zīme-->
	<br>
    <button type="button" id="*" onclick="myFunction(this.id)">x</button> <!--Reizināšanas zīme-->
	<br>
    <button type="button" id="/" onclick="myFunction(this.id)">÷</button> <!--Dalīšanas zīme-->
	<br>
    <button type="button" id="+" onclick="myFunction(this.id)">+</button> <!--Plusa zīme-->
	<br>
    <button type="button" id="-" onclick="myFunction(this.id)">-</button> <!--Mīnusa zīme-->
	<br>
	<button type="button" id="SQRT" onclick="doMath()">√</button> <!--Kvadrātsakne-->
	<br>
	<button type="button" id="imp*inp" onclick="compute()"><sub>2</sub></button> <!--Kvadrāts-->
</div>
<br>
</table>
</center>
</div>
<center>
<p>Special thanks to my peeps at <a href="http://www.stackoverflow.com">StackOverFlow</a> for helping me with some issues!</p> <!--Pateicības piezīme-->
</center>
</body>
</html>

【问题讨论】:

  • 给他们一个类名,使用getElementsByClassName()和一个for循环来为所有具有该类名的元素设置新的css?
  • 不会有关于此的堆栈溢出教程。首先,您需要使用表格或引导程序在 html 中整齐地格式化您的按钮。然后你只需在css中设置高度/宽度

标签: javascript calculator calc


【解决方案1】:

尝试添加一些 CSS:

button {
    width: 40px;
    height: 40px;
}

【讨论】:

    【解决方案2】:

    你可以通过 CSS 做到这一点,不需要 JS:

    .symbols button{
      width: 35px;
      height: 35px;
    }
    

    这针对符号 div 中的所有按钮。

    你也可以让它更通用:

    button{
      width: 35px;
      height: 35px;
    }
    

    这将为页面上的每个按钮设置样式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-20
      • 2013-04-29
      • 2014-03-07
      • 1970-01-01
      • 2018-05-31
      • 2016-11-24
      • 1970-01-01
      • 2016-09-16
      相关资源
      最近更新 更多