(转载)长用JS

 

学了点新的知识:

俺也加点自己的JS 技巧:
1.访问html 标记的自定义属性
<input  id="aa" value="i love you!"  msg="Message from attribute"> 
alert(aa.getAttribute(
"msg"));
alert(aa.getAttribute(
"value"));
aa.setAttribute(
"msg","someAttribute");

2.使用 BUTTON 创建带下划线的 按钮。
<button  onclick='fnRemoveChild()' ctrlKey='j' accessKey='A'>Delete <u>(j)</u></button>

3. 给字符串加上原型函数
String.prototype.startsWith 
= function(startWith)
{
var len = startWith.length;

if(this.substring(0,len) == startWith)
{
return true;
}

else
{
return false;
}

}
;

String.prototype.startsWithIgnoreCase 
= function(startWith)
{
var strCopy = this.toLowerCase();
var len = startWith.length;

if(this.substring(0,len) == startWith.toLowerCase())
{
return true;
}

else
{
return false;
}

}
;

String.prototype.endsWith 
= function(endWith)
{
if(this.substring(this.indexOf(endWith)) == endWith)
{
return true;
}

else
{
return false;
}

}
;

String.prototype.endsWithIgnoreCase 
= function(endWith)
{

var strCopy = this.toLowerCase();

if(strCopy.substring(strCopy.indexOf(endWith.toLowerCase())).toLowerCase() == endWith.toLowerCase())
{
return true;
}

else
{
return false;
}

}
;

4. 四舍五入 函数:
/**//**
* floatVal==> 
* exp ==> 精度。 小数点后面几位, 如果不要小数点,参数可以设置为0.
*/

function fnRound(floatVal,exp)
{
  
return Math.round(floatVal * Math.pow(10, exp)) /Math.pow(10, exp) +"";
}


数字转换成大写金额函数   [ 日期:
2005-05-09 ]   [ 来自: ]非常经典的数字转换成大写金额函数


引用内容:
<script language="javascript">
<!--
// 数字转换成大写金额函数
function atoc(numberValue){
    
var numberValue=new String(Math.round(numberValue*100)); // 数字金额
    var chineseValue="";          // 转换后的汉字金额
    var String1 = "零壹贰叁肆伍陆柒捌玖";       // 汉字数字
    var String2 = "万仟佰拾亿仟佰拾万仟佰拾元角分";     // 对应单位
    var len=numberValue.length;         // numberValue 的字符串长度
    var Ch1;             // 数字的汉语读法
    var Ch2;             // 数字位的汉字读法
    var nZero=0;            // 用来计算连续的零值的个数
    var String3;            // 指定位置的数值
    if(len>15){
        alert(
"超出计算范围");
        
return "";
    }

    
if (numberValue==0){
        chineseValue 
= "零元整";
        
return chineseValue;
    }


    String2 
= String2.substr(String2.length-len, len);   // 取出对应位数的STRING2的值
    for(var i=0; i<len; i++){
        String3 
= parseInt(numberValue.substr(i, 1),10);   // 取出需转换的某一位的值
        if ( i != (len - 3&& i != (len - 7&& i != (len - 11&& i !=(len - 15) ){
            
if ( String3 == 0 ){
                Ch1 
= "";
                Ch2 
= "";
                nZero 
= nZero + 1;
            }

            
else if ( String3 != 0 && nZero != 0 ){
                Ch1 
= "" + String1.substr(String3, 1);
                Ch2 
= String2.substr(i, 1);
                nZero 
= 0;
            }

            
else{
                Ch1 
= String1.substr(String3, 1);
                Ch2 
= String2.substr(i, 1);
                nZero 
= 0;
            }

        }

        
else{              // 该位是万亿,亿,万,元位等关键位
            if( String3 != 0 && nZero != 0 ){
                Ch1 
= "" + String1.substr(String3, 1);
                Ch2 
= String2.substr(i, 1);
                nZero 
= 0;
            }

            
else if ( String3 != 0 && nZero == 0 ){
                Ch1 
= String1.substr(String3, 1);
                Ch2 
= String2.substr(i, 1);
                nZero 
= 0;
            }

            
else if( String3 == 0 && nZero >= 3 ){
                Ch1 
= "";
                Ch2 
= "";
                nZero 
= nZero + 1;
            }

            
else{
                Ch1 
= "";
                Ch2 
= String2.substr(i, 1);
                nZero 
= nZero + 1;
            }

            
if( i == (len - 11|| i == (len - 3)){    // 如果该位是亿位或元位,则必须写上
                Ch2 = String2.substr(i, 1);
            }

        }

        chineseValue 
= chineseValue + Ch1 + Ch2;
    }


    
if ( String3 == 0 ){           // 最后一位(分)为0时,加上“整”
        chineseValue = chineseValue + "";
    }


    
return chineseValue;
}

//-->
</script>

<script>
    document.write(
"1234567.89转换成大写为:"+atoc("1234567.89"));
</script>

动态增加
/删除文件域 


<input type=button value="Add" onclick='additem("tb")'>
<table id="tb"></table>
<script language="javascript">
function additem(id){
    
var row,cell,str;
    row 
= eval("document.all["+'"'+id+'"'+"]").insertRow();
    
if(row != null ){
        cell 
= row.insertCell();
        str
="<input type="+'"'+"file"+'"'+" name="+'"'+"addfile"+'"'+"><input type="+'"'+"button"+'"'+" value="+'"'+"delete"+'"'+" onclick='deleteitem(this,"+'"'+"tb"+'"'+");'>"
        cell.innerHTML
=str;
    }

}

function deleteitem(obj,id){
    
var rowNum,curRow;
    curRow 
= obj.parentNode.parentNode;
    rowNum 
= eval("document.all."+id).rows.length - 1;
    eval(
"document.all["+'"'+id+'"'+"]").deleteRow(curRow.rowIndex);
}

</script>

只能输入汉字
<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))">
只能输入全角
<input onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))">
只能输入数字
<input onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
只能输入英文和数字
<input onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
一些常用的表单提交检测函数

function getlenb(tstr)
{
j
=0
for (i=0;i<tstr.length;i++)
{
        
if(tstr.charCodeAt(i)>255)
                j
++;
        j
++;

}

return j;
}



function checkint(tstr)
{
re
=/[^0-9]/;
rp
=tstr.search(re); 
return rp;
}

function checkhz(tstr)
{
for (i=0;i<tstr.lengt;i++)
        
if( tstr.charCodeAt(i)>255 )
                
{
                        
return 1;
                        
break;
                }

//alert(String.fromCharCode(258))
return 0;

}

function checkstr(tstr)
{
//re=/[^a-zA-Z0-9_]/;
re=/\W/;
rp
=tstr.search(re); 
return rp;

}

function checkhtml(tstr)
{
//re=/[^a-zA-Z0-9_]/;
re=/<(.*)>.*<\/\1>/;
rp
=tstr.search(re); 
return rp;
}

function checkblank(tstr)
{
//re=/[^a-zA-Z0-9_]/;
re=/\s/;
rp
=tstr.search(re); 
return rp;
}

function checkemail(tstr)
{
re1
=/([\w\-\.])+[@]{1}([\w\-])+(\.[\w\-])+/;
rp1
=tstr.search(re1); 
re2
=/[^a-zA-Z0-9_\.\-\@]/;
rp2
=tstr.search(re2);
//alert(rp1)
if (rp1==-1||rp2!=-1)
        
return false;        
else
        
return true;
}

function checksqlstr(tstr)
{
re1
=/[\^\s',\s\$]/;
rp1
=tstr.search(re1);
//rp2=tstr.search(re2);
if (rp1!=-1)
        
return false
else 
        
return true;
        
}
posted @ 2007-05-11 16:34  VincentYinBo  阅读(582)  评论(0编辑  收藏  举报