学了点新的知识:
![](/Images/OutliningIndicators/None.gif)
俺也加点自己的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");
![](/Images/OutliningIndicators/None.gif)
2.使用 BUTTON 创建带下划线的 按钮。
<button onclick='fnRemoveChild()' ctrlKey='j' accessKey='A'>Delete <u>(j)</u></button>
![](/Images/OutliningIndicators/None.gif)
3. 给字符串加上原型函数
String.prototype.startsWith = function(startWith)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
var len = startWith.length;
![](/Images/OutliningIndicators/InBlock.gif)
if(this.substring(0,len) == startWith)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return true;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return false;
}
};
![](/Images/OutliningIndicators/None.gif)
String.prototype.startsWithIgnoreCase = function(startWith)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
var strCopy = this.toLowerCase();
var len = startWith.length;
![](/Images/OutliningIndicators/InBlock.gif)
if(this.substring(0,len) == startWith.toLowerCase())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return true;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return false;
}
};
![](/Images/OutliningIndicators/None.gif)
String.prototype.endsWith = function(endWith)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
if(this.substring(this.indexOf(endWith)) == endWith)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return true;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return false;
}
};
![](/Images/OutliningIndicators/None.gif)
String.prototype.endsWithIgnoreCase = function(endWith)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
var strCopy = this.toLowerCase();
![](/Images/OutliningIndicators/InBlock.gif)
if(strCopy.substring(strCopy.indexOf(endWith.toLowerCase())).toLowerCase() == endWith.toLowerCase())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return true;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return false;
}
};
![](/Images/OutliningIndicators/None.gif)
4. 四舍五入 函数:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**//**//**
* floatVal==>
* exp ==> 精度。 小数点后面几位, 如果不要小数点,参数可以设置为0.
*/
function fnRound(floatVal,exp)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
return Math.round(floatVal * Math.pow(10, exp)) /Math.pow(10, exp) +"";
}
![](/Images/OutliningIndicators/None.gif)
数字转换成大写金额函数 [ 日期:2005-05-09 ] [ 来自: ]非常经典的数字转换成大写金额函数
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
引用内容:
<script language="javascript">
<!--
// 数字转换成大写金额函数
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
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; // 指定位置的数值
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(len>15)
{
alert("超出计算范围");
return "";
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (numberValue==0)
{
chineseValue = "零元整";
return chineseValue;
}
![](/Images/OutliningIndicators/InBlock.gif)
String2 = String2.substr(String2.length-len, len); // 取出对应位数的STRING2的值
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i=0; i<len; i++)
{
String3 = parseInt(numberValue.substr(i, 1),10); // 取出需转换的某一位的值
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( i != (len - 3) && i != (len - 7) && i != (len - 11) && i !=(len - 15) )
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( String3 == 0 )
{
Ch1 = "";
Ch2 = "";
nZero = nZero + 1;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if ( String3 != 0 && nZero != 0 )
{
Ch1 = "零" + String1.substr(String3, 1);
Ch2 = String2.substr(i, 1);
nZero = 0;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
Ch1 = String1.substr(String3, 1);
Ch2 = String2.substr(i, 1);
nZero = 0;
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{ // 该位是万亿,亿,万,元位等关键位
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if( String3 != 0 && nZero != 0 )
{
Ch1 = "零" + String1.substr(String3, 1);
Ch2 = String2.substr(i, 1);
nZero = 0;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if ( String3 != 0 && nZero == 0 )
{
Ch1 = String1.substr(String3, 1);
Ch2 = String2.substr(i, 1);
nZero = 0;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if( String3 == 0 && nZero >= 3 )
{
Ch1 = "";
Ch2 = "";
nZero = nZero + 1;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
Ch1 = "";
Ch2 = String2.substr(i, 1);
nZero = nZero + 1;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if( i == (len - 11) || i == (len - 3))
{ // 如果该位是亿位或元位,则必须写上
Ch2 = String2.substr(i, 1);
}
}
chineseValue = chineseValue + Ch1 + Ch2;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( String3 == 0 )
{ // 最后一位(分)为0时,加上“整”
chineseValue = chineseValue + "整";
}
![](/Images/OutliningIndicators/InBlock.gif)
return chineseValue;
}
//-->
</script>
![](/Images/OutliningIndicators/None.gif)
<script>
document.write("1234567.89转换成大写为:"+atoc("1234567.89"));
</script>
![](/Images/OutliningIndicators/None.gif)
动态增加/删除文件域
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
<input type=button value="Add" onclick='additem("tb")'>
<table id="tb"></table>
<script language="javascript">
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
function additem(id)
{
var row,cell,str;
row = eval("document.all["+'"'+id+'"'+"]").insertRow();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(row != null )
{
cell = row.insertCell();
str="<input type="+'"'+"file"+'"'+" name="+'"'+"addfile"+'"'+"><input type="+'"'+"button"+'"'+" value="+'"'+"delete"+'"'+" onclick='deleteitem(this,"+'"'+"tb"+'"'+");'>"
cell.innerHTML=str;
}
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
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>
![](/Images/OutliningIndicators/None.gif)
只能输入汉字
<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,''))">
一些常用的表单提交检测函数
![](/Images/OutliningIndicators/None.gif)
function getlenb(tstr)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
j=0
for (i=0;i<tstr.length;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(tstr.charCodeAt(i)>255)
j++;
j++;
![](/Images/OutliningIndicators/InBlock.gif)
}
return j;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
function checkint(tstr)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
re=/[^0-9]/;
rp=tstr.search(re);
return rp;
}
function checkhz(tstr)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
for (i=0;i<tstr.lengt;i++)
if( tstr.charCodeAt(i)>255 )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 1;
break;
}
//alert(String.fromCharCode(258))
return 0;
![](/Images/OutliningIndicators/InBlock.gif)
}
function checkstr(tstr)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//re=/[^a-zA-Z0-9_]/;
re=/\W/;
rp=tstr.search(re);
return rp;
![](/Images/OutliningIndicators/InBlock.gif)
}
function checkhtml(tstr)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//re=/[^a-zA-Z0-9_]/;
re=/<(.*)>.*<\/\1>/;
rp=tstr.search(re);
return rp;
}
function checkblank(tstr)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//re=/[^a-zA-Z0-9_]/;
re=/\s/;
rp=tstr.search(re);
return rp;
}
function checkemail(tstr)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
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)
评论()
编辑
收藏
举报