<script>
???extend?=?function?(a,?b)?{?//合并對(duì)象
??????if?(typeof?a?!==?"object"?||?typeof?b?!==?"object")?{
?????????return;
??????}
??????for?(key?in?b)?{
?????????a[key]?=?b[key];
??????}
??????return?a;
???};
?
???extend(Math,?{
??????/*************格式化數(shù)值***************/
??????format:?function?(num,?style)?{
?????????if?(!/#+([^#]+)?(#+)(?:\.(#+))?/.test(style))?{
????????????return;
?????????}
?????????//分割符
?????????var?split?=?RegExp.$1;
?????????//保留小數(shù)位數(shù)
?????????num?=?parseFloat(num).toFixed(RegExp.$3.length);
?????????var?re?=?new?RegExp('(\\d{1,'?+??RegExp.$2.length?+?'})(?=(\\d{'?+??RegExp.$2.length?+?'})+(?:$|\\.))',?'g');
?????????//console.log(re);
?????????if(!/(-?\d+)(?:\.(\d+))?/.test(num)){
????????????return;
?????????}
?????????var?d?=?(RegExp.$2???'.'?+?RegExp.$2?:?''),
???????????????str?=?RegExp.$1.replace(re,?function?(a,?b,?c,?d)?{
??????????????????return?b?+?split;
???????????????});
?????????return?str?+?d;
??????}
???});
?
???$(function(){
??????var?total?=?0;
??????$('.int').each(function(){
?????????$(this).keypress(function(event){
????????????if(!((event.charCode?>=?48?&&?event.charCode?<=?57)?||?event.charCode?===?46?||?event.charCode?===?45?||?event.charCode?===?0)){
???????????????if(event.preventDefault){
??????????????????event.preventDefault()
???????????????}else{
??????????????????event.returnValue?=?false;
???????????????}
????????????}
?????????}).keyup(function(){
????????????total?=?0;
????????????$('.int').each(function(){
???????????????if($(this).val()?===?'.'?||?$(this).val()?===?'-'){
??????????????????return;
???????????????}
???????????????total?+=?parseFloat($(this).val()?||?0);
????????????});
?
????????????$('.total').html(Math.format(total,'#,###.##'));
?????????});
??????});
???});
</script>
<body>
<div>
???<input?type="text"?class="int">
???<input?type="text"?class="int">
???<input?type="text"?class="int">
???<input?type="text"?class="int">
???<input?type="text"?class="int">
???<input?type="text"?class="int">
???<input?type="text"?class="int">
???<input?type="text"?class="int">
???總和:<div?class="total"></div>
</div>
</body>
JS,如何將這段加法運(yùn)算代碼,改成減法運(yùn)算?
阿哥我害怕
2017-04-07 14:45:43