可能需要将所有数据保存在数组中吗? 并通过正则表达式拉出我需要的东西
function set(num) {
document.getElementById("calcAll").innerText += num;
}
function operator(e) {
var expression = document.getElementById("calcAll").innerText,
result = document.getElementById("result"),
calcAll = document.getElementById("calcAll");
switch (e) {
case "c":
calcAll.innerText = '';
result.innerText = '';
break;
case "=":
try {
result.innerText = eval(expression);
calcAll.innerText = expression + '=' + eval(expression);
} catch (e) {
alert('неа');
}
break;
case "+/-":
calcAll.innerText = expression * -1;
break;
case "xy":
//
//
//
//
result.innerText = Math.pow(x, y);
calcAll.innerText = Math.pow(x, y);
//
//
//
//
break;
}
}
.screen {
background: #000;
color: #fff;
}
#result {
font-size: 25px;
}
.red {
background: red;
}
<div class="screen">
<div id="result"></div>
<div id="calcAll"></div>
</div>
<br>
<button onclick="operator('=')">=</button>
<button onclick="set(1)">1</button>
<button onclick="set(2)">2</button>
<button onclick="set(3)">3</button>
<button onclick="set(4)">4</button>
<button onclick="set(5)">5</button>
<button onclick="set(6)">6</button>
<button onclick="set(7)">7</button>
<button onclick="set(8)">8</button>
<button onclick="set(9)">9</button>
<button onclick="set(0)">0</button>
<div>
<button onclick="operator('c')">C</button>
<button onclick="set('%')">%</button>
<button onclick="set('-')">-</button>
<button onclick="set('+')">+</button>
<button onclick="set('/')">/</button>
<button onclick="set('*')">x</button>
<button onclick="set('.')">.</button>
<button onclick="operator('+/-')">+/-</button>
<button class="red" onclick="operator('xy')">xy</button>
</div>
答案 0 :(得分:0)
我不知道我是否得到你想要的,但我想你想知道如何计算战俘,对吗?
我试着像你做其他作品那样做。
function set(num) {
document.getElementById("calcAll").innerText += num;
}
function operator(e) {
var expression = document.getElementById("calcAll").innerText,
result = document.getElementById("result"),
calcAll = document.getElementById("calcAll");
switch (e) {
case "c":
calcAll.innerText = '';
result.innerText = '';
break;
case "=":
try {
if (expression.indexOf("xy") > -1) {
var arrExp = expression.split("xy");
var num1 = eval(arrExp[0]);
var num2 = eval(arrExp[1]);
result.innerText = Math.pow(arrExp[0], arrExp[1]);
calcAll.innerText = expression + '=' + Math.pow(arrExp[0], arrExp[1]);
} else {
result.innerText = eval(expression);
calcAll.innerText = expression + '=' + eval(expression);
}
} catch (e) {
alert('неа');
}
break;
case "+/-":
calcAll.innerText = expression * -1;
break;
case "xy":
//
//
//
//
result.innerText = Math.pow(x, y);
calcAll.innerText = Math.pow(x, y);
//
//
//
//
break;
}
}
&#13;
.screen {
background: #000;
color: #fff;
}
#result {
font-size: 25px;
}
.red {
background: red;
}
&#13;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="screen">
<div id="result"></div>
<div id="calcAll"></div>
</div>
<br>
<button onclick="operator('=')">=</button>
<button onclick="set(1)">1</button>
<button onclick="set(2)">2</button>
<button onclick="set(3)">3</button>
<button onclick="set(4)">4</button>
<button onclick="set(5)">5</button>
<button onclick="set(6)">6</button>
<button onclick="set(7)">7</button>
<button onclick="set(8)">8</button>
<button onclick="set(9)">9</button>
<button onclick="set(0)">0</button>
<div>
<button onclick="operator('c')">C</button>
<button onclick="set('%')">%</button>
<button onclick="set('-')">-</button>
<button onclick="set('+')">+</button>
<button onclick="set('/')">/</button>
<button onclick="set('*')">x</button>
<button onclick="set('.')">.</button>
<button onclick="operator('+/-')">+/-</button>
<button class="red" onclick="set('xy')">xy</button>
</div>
</body>
</html>
&#13;
见这里:https://jsbin.com/cabilehuvi/edit?js,console,output
例如:点击3,然后点击xy,然后点击2点,然后点击等号。你会得到9。