javascript函数(格式化数字,日期比较,光标处插入或修改文字,JS图片动画效果)

javascript函数(格式化数字,日期比较,光标处插入或修改文字,JS图片动画效果)
转自: http://hi.baidu.com/lael80/blog/item/d0bd46df1941d31248540363.html

javascript格式化数字,如 8888.85745 格式化成 8,888.86

<html>
<head>
<title> New Document </title>
</head>

<body>
<script language="JavaScript">
<!--
//lael 2005-11-09
function formatnumber(fnumber,fdivide,fpoint,fround){

    var fnum = fnumber + '';
    var revalue="";

    if(fnum==null){
        for(var i=0;i<fpoint;i++)revalue+="0";
        return "0."+revalue;
    }
    fnum = fnum.replace(/^\s*|\s*$/g,'');
    if(fnum==""){
        for(var i=0;i<fpoint;i++)revalue+="0";
        return "0."+revalue;
    }

    fnum=fnum.replace(/,/g,"");

    if(fround){
        var temp = "0.";
        for(var i=0;i<fpoint;i++)temp+="0";
        temp += "5";

        fnum = Number(fnum) + Number(temp);
        fnum += '';
    }

    var arrayf=fnum.split(".");

    if(fdivide){
        if(arrayf[0].length>3){
            while(arrayf[0].length>3){
                revalue=","+arrayf[0].substring(arrayf[0].length-3,arrayf[0].length)+revalue;
                arrayf[0]=arrayf[0].substring(0,arrayf[0].length-3);
            }
        }
    }
    revalue=arrayf[0]+revalue;

    if(arrayf.length==2&&fpoint!=0){
        arrayf[1]=arrayf[1].substring(0,(arrayf[1].length<=fpoint)?arrayf[1].length:fpoint);

        if(arrayf[1].length<fpoint)
            for(var i=0;i<fpoint-arrayf[1].length;i++)arrayf[1]+="0";
        revalue+="."+arrayf[1];
    }else if(arrayf.length==1&&fpoint!=0){
        revalue+=".";
        for(var i=0;i<fpoint;i++)revalue+="0";
    }

    return revalue;
}
//-->
</script>

<input name="text1" type="text" value="" onblur="this.value=formatnumber(this.value, true, 2, true)" />
</body>
</html>







//日期比较
function datecompare(date1, date2){
var d1 = new Date(date1.replace(/\-/g, "\/"));
var d2 = new Date(date2.replace(/\-/g, "\/"));

var flag = true;

if(isNaN(d1) || isNaN(d2))flag = false;//不是日期

if(flag &&
d1.getFullYear() > d2.getFullYear()){
flag = false;
}
if(flag &&
d1.getFullYear() == d2.getFullYear() &&
d1.getMonth() > d2.getMonth()){
flag = false;
}
if(flag &&
d1.getFullYear() == d2.getFullYear() &&
d1.getMonth() == d2.getMonth() &&
d1.getDate() > d2.getDate()){
flag = false;
}
return flag;
}



//cookie


function getcookievalue(sname){
var svalue="";
var sname=sname+"=";
if(document.cookie.length>0){
offset=document.cookie.indexOf(sname);
if(offset!=-1){
offset+=sname.length;
end=document.cookie.indexOf(";",offset);
if(end==-1)end=document.cookie.length;
svalue=unescape(document.cookie.substring(offset,end))
}
}
return svalue;
}

function setcookievalue(sname,svalue){
var expire="";
expire=new Date((new Date()).getTime()+31536000);
expire="; expires="+expire.toGMTString();
document.cookie=sname+"="+escape(svalue)+expire;
}



光标处插入或修改文字

function storeCaret(_obj1){
    if(_obj1.createTextRange)
        _obj1.caretPos = document.selection.createRange().duplicate();  
}

function insertAtCaret() {
    var _obj1 = document.getElementById("textarea1");
       
    var _text = "[b]";
   
    _obj1.focus();
    storeCaret(_obj1);
   
    if(_obj1.createTextRange && _obj1.caretPos){
        var caretPos = _obj1.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?_text + ' ' : _text;     
    }else _obj1.value = _text;
}






JS图片动画效果
效果:http://www.carsclub.com.cn/

<style>
#flash_img {
    filter: revealtrans(duration=2.0,transition=12); width: 100%;
}
</style>

<table border="0" align="right" cellpadding="0" cellspacing="0">
                    <tr>
                      <td class="focus-ctl-nows" id="flash_show_ctl_0" onClick="showImage(0);">1</td>

                      <td class="focus-ctl" id="flash_show_ctl_1" onClick="showImage(1);">2</td>
                      <td class="focus-ctl" id="flash_show_ctl_2" onClick="showImage(2);">3</td>
                      <td class="focus-ctl" id="flash_show_ctl_3" onClick="showImage(3);">4</td>
                      <td class="focus-ctl" id="flash_show_ctl_4" onClick="showImage(4);">5</td>
                      <td>&nbsp;</td>
                    </tr>
                  </table>

<div id="flash_img" style="width:334px; height:167px;"></div>

<script language="javascript">
<!--
////////////////begin
var showImageIndex = -1;
var imageTimer = null;
function showImage(imageIndex)
{
    var flash_img_div = document.getElementById("flash_img");
   
    if(imageIndex>=fImgs.length){
        imageIndex = 0;
    }
   
    if(!fImgs[imageIndex] || imageIndex==showImageIndex)return false;   
   
    var imgId = "__fImg"+imageIndex;
    flash_img_div.filters && flash_img_div.filters[0].Apply();
    for(var i=0; i<flash_img_div.childNodes.length; i++){
        flash_img_div.removeChild(flash_img_div.childNodes[i]);
    }
    for(var i=0; i<fImgs.length;i++){
        if(i == imageIndex)continue;
        document.getElementById("flash_show_ctl_" + i).className = "focus-ctl";   
    }
    document.getElementById("flash_show_ctl_" + imageIndex).className = "focus-ctl-nows";
    var pos = fImgs[imageIndex].img.lastIndexOf(".");
    if( fImgs[imageIndex].img.substr(pos+1).substr(0,3).toLowerCase()=="swf" ){
        flash_img_div.innerHTML += '\
            <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="334" height="167" id="'+imgId+'">\
                <param name="movie" value="'+fImgs[imageIndex].img+'" />\
                <param name="quality" value="high" />\
                <param name="wmode" value="transparent">\
                <param name="scale" value="exactfit">\
                <embed src="'+fImgs[imageIndex].img+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="334" height="167"></embed>\
            </object>';
    }else{
        var img = new Image();
        img.border = "0";
        img.src = fImgs[imageIndex].img;
        img.width = "334";
        img.height = "167";
        var a = document.createElement("a");
        a.href = fImgs[imageIndex].href;
        a.title = fImgs[imageIndex].title;
        a.target = "_blank";
        a.id = imgId;
        a.appendChild(img);
        flash_img_div.appendChild(a);
    }
    flash_img_div.filters && flash_img_div.filters[0].Play();
    showImageIndex = imageIndex;
   
    return true;
}
function imagePlay()
{
    if(imageTimer) return;
    if(showImageIndex>=fImgs.length){
        showImageIndex = -1;
    }
    showImage(showImageIndex+1);
    imageTimer = setInterval(function(){
                    var stat = showImage(showImageIndex+1);
                    if(!stat){
                        imageStop();
                    }   
                },8000);
}
function imageStop(){
    if(imageTimer)clearInterval(imageTimer);
    imageTimer = null;
}
function showNextImage(){
    showImage(showImageIndex+1);
}
function showPrevImage(){
    showImage(showImageIndex-1);
}

var fImgs = new Array();
fImgs[0] = {title:"",img:"upload/images/subject/2007-02-06/1174915891_outline.jpg",href:"news.php?id=220"};
fImgs[1] = {title:"",img:"upload/images/subject/2007-02-06/1175244094_outline.jpg",href:"news.php?id=212"};
fImgs[2] = {title:"",img:"upload/images/subject/2007-02-06/1177150583_outline.jpg",href:"news.php?id=219"};
fImgs[3] = {title:"",img:"upload/images/subject/2007-02-06/1175897263_outline.jpg",href:"news.php?id=217"};
fImgs[4] = {title:"",img:"upload/images/subject/2007-02-06/1177509461_outline.jpg",href:"news.php?id=213"};
////////////////end

window.document.body.onload = new function(){
    imagePlay();//焦点图文
}
//-->
</script>

你可能感兴趣的:(javascript函数(格式化数字,日期比较,光标处插入或修改文字,JS图片动画效果))