demo:http://codepen.io/tianzi77/pen/YXdXjb
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
color: #999;
font: 12px/1.5 Tahoma;
}
#outer {
width: 500px;
margin: 0 auto;
}
#outer input {
padding: 3px;
border: 1px solid #ccc;
font-family: inherit;
width: 220px;
margin-right: 10px;
}
.sum {
font-size: 30px;
color: #abcdef;
}
</style>
<script>
window.onload = function () {
var oButton = document.getElementsByTagName(‘button‘)[0];
var oStrong = document.getElementsByTagName(‘strong‘)[0];
var oInput = document.getElementsByTagName(‘input‘)[0];
oButton.onclick = function () {
var sum = 0;
var oInput1 = document.getElementsByTagName(‘input‘)[0].value.split(‘+‘);
for (var i in oInput1) {
sum += parseInt(oInput1[i])
}
oStrong.innerHTML = sum;
}
}
</script>
</head>
<body>
<div id="outer">
<label for="">
<input type="text" value="1+2+3+4+5+6+7+8+9+10"><span>输入数字求和,数字之间用半角"+"号分隔</span>
</label>
<p>
<button>求和</button>
</p>
<strong class="sum"></strong>
</div>
</body>
</html>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u011263845/article/details/47173003