论坛首页 Web前端技术论坛

我的常用Javascript操作收集

浏览 8371 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-10-07   最后修改:2009-01-16
  • 小写金额与大写金额联动
<script language="JavaScript">

function daxie()
{
  this.values = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
  this.digits = ["", "拾", "佰", "仟"];
}

function daxie.prototype.getdx(num)
{
  if(isNaN(num)) return "";
  var number = Math.round(num*100)/100;
  number = number.toString(10).split('.');
  var integer = number[0];
  var len = integer.length;
  if (len > 12)
    return "数值超出范围!支持的最大数值为 999999999999.99";
  var returnValue = this.bns(integer.slice(-4));
  if (len > 4)
    returnValue = this.bns(integer.slice(-8,-4)) + (integer.slice(-8,-4)!="0000"?"万":"") + returnValue;
  if (len > 8)
    returnValue = this.bns(integer.slice(-12,-8)) + "亿" + returnValue;
  if(returnValue!="")
    returnValue += "圆";
  if(number.length==2)
  {
    var cok = number[1].split('');
    if(returnValue!="" || cok[0]!="0")
      returnValue += this.values[parseInt(cok[0])] + (cok[0]!="0"?"角":"");
    if(cok.length>=2)
      returnValue += this.values[parseInt(cok[1])] + "分";
  }
  if(returnValue!="" && !/分$/.test(returnValue))
    returnValue += "整";
  return returnValue;
}

function daxie.prototype.bns(str)
{
  var num = str.split('');
  var dsl = num.length-1;
  var returnValue = "";
  for (var i=0; i<=dsl; i++)
    returnValue += this.values[parseInt(num[i])] + (num[i]!='0'?this.digits[dsl-i]:"");
  returnValue = returnValue.replace(/零+$/, "").replace(/零{2,}/, "零");
  return returnValue;
}

//////////////////////////////////////////////
var stmp = "";
var dfs = new daxie();
function nst(t)
{
  if(t.value==stmp) return;
  var ms = t.value.replace(/[^\d\.]/g,"").replace(/(\.\d{2}).+$/,"$1");
  var txt = ms.split(".");
  while(/\d{4}(,|$)/.test(txt[0]))
    txt[0] = txt[0].replace(/(\d)(\d{3}(,|$))/,"$1,$2");
  t.value = stmp = txt[0]+(txt.length>1?"."+txt[1]:"");
  bbb.innerHTML ="<font color=red>"+dfs.getdx(parseFloat(ms))+"</font>";
}

</script>
小写金额:<input type="text" name="aaa" onkeyup="nst(this)"><br>
大写金额: <SPAN id="bbb"> </SPAN>
 
  • 获取文本框里鼠标选取到的文字
<textarea rows="10" cols="50" id="t1">
请问如何获得 文本框里 鼠标选取到的文字?
</textarea>
<br>
<button onClick="if (document.selection.createRange().text != '') t1.value = t1.value.replace(document.selection.createRange().text, '<b>' + document.selection.createRange().text + '</b>')"><b> B </b></button>
<button onClick="if (document.selection.createRange().text != '') t1.value = t1.value.replace(document.selection.createRange().text, '<i>' + document.selection.createRange().text + '</i>')"><i> I </i></button>
  •   复选框的全选,多选,全不选,反选
<form name=hrong>
<input type=checkbox name=All onclick="checkAll('mm')">全选<br/>
<input type=checkbox name=mm onclick="checkItem('All')"><br/>
<input type=checkbox name=mm onclick="checkItem('All')"><br/>
<input type=checkbox name=mm onclick="checkItem('All')"><br/>
<input type=checkbox name=mm onclick="checkItem('All')"><br/>
<input type=checkbox name=mm onclick="checkItem('All')"><br/><br/>

<input type=checkbox name=All2 onclick="checkReverse('mm2')">反选<br/>
<input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
<input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
<input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
<input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
<input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
<input type=checkbox name=All3 onclick="checkItem('mm3')">特选<br/>
<input type=checkbox name=mm3 onclick="checkItem('All3')"><br/>


</form>

<SCRIPT LANGUAGE="JavaScript">
function checkAll(str)
{
  var a = document.getElementsByName(str);
  var n = a.length;
  for (var i=0; i<n; i++)
  a[i].checked = window.event.srcElement.checked;
}

function checkReverse(str)
{
  var a = document.getElementsByName(str);
  var n = a.length;
  for (var i=0; i<n; i++)
  a[i].checked = !a[i].checked;
}

function checkItem(str)
{
  var e = window.event.srcElement;
  var all = eval("document.hrong."+ str);
  if (e.checked)
  {
    var a = document.getElementsByName(e.name);
    all.checked = true;
    for (var i=0; i<a.length; i++)
    {
      if (!a[i].checked){ all.checked = false; break;}
    }
  }
  else all.checked = false;
}
</SCRIPT>
   发表时间:2008-10-07  
滚动条滚动至最底端:
document.body.scrollTop = document.body.scrollHeight;
0 请登录后投票
   发表时间:2008-10-07  
得到中文笔画数
哪里会用到这个东西啊.....
0 请登录后投票
   发表时间:2008-10-07  
中文笔画数 低效到极点的方法啊 哈哈
0 请登录后投票
   发表时间:2008-10-07  

囧TZ
0 请登录后投票
   发表时间:2008-10-08  
转中文大写的有点小问题,比如14行很明显应该是numbernumber[0]
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics