html body script document createElement div document body appendChild

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<html>
<body>
<script>
d = document.createElement('div');
document.body.appendChild(d);
var frm = document.createElement('form');
d.appendChild(frm);
var now_input = document.createElement('input');
now_input.id = 'input';
document.body.appendChild(now_input);
var now_input = document.createElement('input');
now_input.id = 'answer';
document.body.appendChild(now_input);
var btn = document.createElement('button');
btn.appendChild(document.createTextNode('Счет'));
frm.appendChild(btn);
btn.onclick = function calculate()
{
var input = document.getElementById('input').value;
var arr = input.split('+');
if (arr.length > 1)
document.getElementById('answer').value = parseInt(arr[0]) + parseInt(arr[1]);
var arr = input.split('*');
if (arr.length > 1)
document.getElementById('answer').value = parseInt(arr[0]) * parseInt(arr[1]);
var arr = input.split('/');
if (arr.length > 1)
document.getElementById('answer').value = parseInt(arr[0]) / parseInt(arr[1]);
var arr = input.split('-');
if (arr.length > 1)
document.getElementById('answer').value = parseInt(arr[0]) - parseInt(arr[1]);
}
</script>
</body>
</html>