单击外部元素时,JavaScript函数切换

时间:2017-11-10 19:33:10

标签: javascript html

我正在尝试构建一个科学的计算器应用程序,但我在第二个功能上被挂断了。我希望能够单击第二个按钮,触发按钮值进行更改,然后在单击任何其他按钮时切换第二个功能。当我点击第二个元素之外时,我已经完成了一切,但是切换了第二个函数。

我使用html表格布局生成“按钮”,第二个按钮是元素。我想在点击任何其他元素时将其切换回来。正如我所建议的那样,我现在已经添加了一些代码但不是完整的代码(我已经删除了有希望的东西),因为它很长。

calculator example

<html>
<head>
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="calc.css">
<script src="calc.js"></script>
</head>

<body>
<p id=myPara></p>
<p id=myAns>0</p>
<table>
<tr id="inputFieldrow">
    <td class="inputField" colspan=5><input id=result readonly></td>
</tr>


<tr>
    <td onclick="shiftFn(1)" id="shiftBtn">2nd</td>
    <td class="blueButton" id="modeBtn">mode</td>
    <td class="blueButton" onclick="del()" id="deleteBtn">delete</td>
    <td class="noBorder"></td>
    <td class="noBorder"></td>
</tr>

 <tr>
     <td class="blueButton" onclick="log(1)" id="logBtn">log</td>
     <td class="badButton" id="prbBtn">prb</td>
     <td class="badButton" id="dataBtn">data</td>
     <td class="noBorder"></td>
     <td class="noBorder"></td>
</tr>

和JavaScript

function shiftFn(state) {
var Btnshift = document.getElementById("shiftBtn"),
    Btnmode = document.getElementById("modeBtn"),
    Btndelete = document.getElementById("deleteBtn"),
    Btnlog = document.getElementById("logBtn");

if (state === 1) {
    Btnshift.setAttribute("onclick", "shiftFn(0)");
    Btnshift.style.backgroundColor = "orange";

    Btnmode.innerHTML = "quit";
    Btndelete.innerHTML = "insert";

    Btnlog.innerHTML = "10<sup>x</sup>";


} else {
    Btnshift.setAttribute("onclick", "shiftFn(1)");
    Btnshift.style.backgroundColor = "#A3D88A";


    Btnmode.innerHTML = "mode";
    Btndelete.innerHTML = "delete";

    Btnlog.innerHTML = "log";
    Btnlog.setAttribute("onclick", "log(1)");
}
}

0 个答案:

没有答案