Robię kalkulator jquery, gdzie klikając przyciski reprezentujące liczby i operatory kalkulatora, innerHTML ekranu zmieni się, odzwierciedlając kliknięte przyciski.Tworzenie kalkulatora JQuery, zmienianie innerHTML, ale nic się nie dzieje
Poniżej jest mój kod:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<title> JQuery Calculator </title>
</head>
<body>
<style>
section {
padding: 2em;
}
.display {
height: 5em;
color: #333;
}
</style>
<section id="calculator">
<div class="display">
</div>
<div id="numbersContainer">
<table id="numbers">
<tr>
<td>
<button class="number" id="one">1</button>
</td>
<td>
<button class="number" id="two">2</button>
</td>
<td>
<button class="number" id="three">3</button>
</td>
</tr>
<tr>
<td>
<button class="number" id="four">4</button>
</td>
<td>
<button class="number" id="five">5</button>
</td>
<td>
<button class="number" id="six">6</button>
</td>
</tr>
<tr>
<td>
<button class="number" id="seven">7</button>
</td>
<td>
<button class="number" id="eight">8</button>
</td>
<td>
<button class="number" id="nine">9</button>
</td>
</tr>
<tr>
<td>
<button class="number" id="zero">0</button>
</td>
</tr>
</table>
<table id="operators">
<tr>
<td>
<button class="operator" id="plus">+</button>
</td>
<td>
<button class="operator" id="minus">-</button>
</td>
</tr>
<tr>
<td>
<button class="operator" id="divide">/</button>
</td>
<td>
<button class="operator" id="times">x</button>
</td>
</tr>
</table>
</div>
</section>
<script>
var storedCalculation;
$('.number, .operator').click(function() {
var numberOrOperator = event.target.innerHTML;
storedCalculation+=numberOrOperator;
});
var calcScreen = $('.display').innerHTML;
calcScreen = storedCalculation;
</script>
</body>
</html>
i tutaj jest jsfiddle niego: http://jsfiddle.net/ynf2qvqw/
Dlaczego jest innerHTML klasy wyświetlacza nie zmienia?
Do Twojej wiadomości, elementy przycisków nie zawierają kodu HTML, więc nie powinieneś używać 'innerHTML'. Mają tylko tekst, więc bezpieczniej byłoby używać treści tekstowej, prawdopodobnie za pomocą '$ (event.target) .text()'. –
spróbuj http://codepen.io/mariusbalaj/pen/bGqhI – Hiero