JS之经典代码段

来源:互联网 发布:保险网络继续教育 编辑:程序博客网 时间:2024/04/28 12:47
小写金额与大写金额联动
  1. <script language="JavaScript">
  2. function daxie()
  3. {
  4.   this.values = ["零""壹""贰""叁""肆""伍""陆""柒""捌""玖"];
  5.   this.digits = ["""拾""佰""仟"];
  6. }
  7. function daxie.prototype.getdx(num)
  8. {
  9.   if(isNaN(num)) return "";
  10.   var number = Math.round(num*100)/100;
  11.   number = number.toString(10).split('.');
  12.   var integer = number[0];
  13.   var len = integer.length;
  14.   if (len > 12)
  15.     return "数值超出范围!支持的最大数值为 999999999999.99";
  16.   var returnValue = this.bns(integer.slice(-4));
  17.   if (len > 4)
  18.     returnValue = this.bns(integer.slice(-8,-4)) + (integer.slice(-8,-4)!="0000"?"万":"") + returnValue;
  19.   if (len > 8)
  20.     returnValue = this.bns(integer.slice(-12,-8)) + "亿" + returnValue;
  21.   if(returnValue!="")
  22.     returnValue += "圆";
  23.   if(number.length==2)
  24.   {
  25.     var cok = number[1].split('');
  26.     if(returnValue!="" || cok[0]!="0")
  27.       returnValue += this.values[parseInt(cok[0])] + (cok[0]!="0"?"角":"");
  28.     if(cok.length>=2)
  29.       returnValue += this.values[parseInt(cok[1])] + "分";
  30.   }
  31.   if(returnValue!="" && !/分$/.test(returnValue))
  32.     returnValue += "整";
  33.   return returnValue;
  34. }
  35. function daxie.prototype.bns(str)
  36. {
  37.   var num = str.split('');
  38.   var dsl = num.length-1;
  39.   var returnValue = "";
  40.   for (var i=0; i<=dsl; i++)
  41.     returnValue += this.values[parseInt(num[i])] + (num[i]!='0'?this.digits[dsl-i]:"");
  42.   returnValue = returnValue.replace(/零+$/, "").replace(/零{2,}/, "零");
  43.   return returnValue;
  44. }
  45. //////////////////////////////////////////////
  46. var stmp = "";
  47. var dfs = new daxie();
  48. function nst(t)
  49. {
  50.   if(t.value==stmp) return;
  51.   var ms = t.value.replace(/[^/d/.]/g,"").replace(/(/./d{2}).+$/,"$1");
  52.   var txt = ms.split(".");
  53.   while(//d{4}(,|$)/.test(txt[0]))
  54.     txt[0] = txt[0].replace(/(/d)(/d{3}(,|$))/,"$1,$2");
  55.   t.value = stmp = txt[0]+(txt.length>1?"."+txt[1]:"");
  56.   bbb.innerHTML ="<font color=red>"+dfs.getdx(parseFloat(ms))+"</font>";
  57. }
  58. </script>
  59. 小写金额:<input type="text" name="aaa" onkeyup="nst(this)"><br>
  60. 大写金额: <SPAN id="bbb"> </SPAN>


获取文本框里鼠标选取到的文字

  1. <textarea rows="10" cols="50" id="t1">
  2. 请问如何获得 文本框里 鼠标选取到的文字?
  3. </textarea>
  4. <br>
  5. <button onClick="if (document.selection.createRange().text != '') t1.value
  6.      = t1.value.replace(document.selection.createRange().text, '<b>' + document.selection.createRange().text + '</b>')">
  7.         <b> B </b></button>
  8. <button onClick="if (document.selection.createRange().text != '') t1.value
  9. = t1.value.replace(document.selection.createRange().text, '<i>' + document.selection.createRange().text + '</i>')"><i> I </i></button>


  复选框的全选,多选,全不选,反选

  1. <form name=hrong>
  2. <input type=checkbox name=All onclick="checkAll('mm')">全选<br/>
  3. <input type=checkbox name=mm onclick="checkItem('All')"><br/>
  4. <input type=checkbox name=mm onclick="checkItem('All')"><br/>
  5. <input type=checkbox name=mm onclick="checkItem('All')"><br/>
  6. <input type=checkbox name=mm onclick="checkItem('All')"><br/>
  7. <input type=checkbox name=mm onclick="checkItem('All')"><br/><br/>
  8. <input type=checkbox name=All2 onclick="checkReverse('mm2')">反选<br/>
  9. <input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
  10. <input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
  11. <input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
  12. <input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
  13. <input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
  14. <input type=checkbox name=All3 onclick="checkItem('mm3')">特选<br/>
  15. <input type=checkbox name=mm3 onclick="checkItem('All3')"><br/>
  16. </form>
  17. <SCRIPT LANGUAGE="JavaScript">
  18. function checkAll(str)
  19. {
  20.   var a = document.getElementsByName(str);
  21.   var n = a.length;
  22.   for (var i=0; i<n; i++)
  23.   a[i].checked = window.event.srcElement.checked;
  24. }
  25. function checkReverse(str)
  26. {
  27.   var a = document.getElementsByName(str);
  28.   var n = a.length;
  29.   for (var i=0; i<n; i++)
  30.   a[i].checked = !a[i].checked;
  31. }
  32. function checkItem(str)
  33. {
  34.   var e = window.event.srcElement;
  35.   var all = eval("document.hrong."+ str);
  36.   if (e.checked)
  37.   {
  38.     var a = document.getElementsByName(e.name);
  39.     all.checked = true;
  40.     for (var i=0; i<a.length; i++)
  41.     {
  42.       if (!a[i].checked){ all.checked = falsebreak;}
  43.     }
  44.   }
  45.   else all.checked = false;
  46. }
  47. </SCRIPT>