javascript一些不錯的函數指令碼代碼_javascript技巧

來源:互聯網
上載者:User

計算字元長度的js函數

複製代碼 代碼如下:

function LEN(str){
var i,sum=0;
for(i=0;i<str.length;i++){
if((str.charCodeAt(i)>=0) && (str.charCodeAt(i)<=255))
sum=sum+1;
else
sum=sum+2;
}
return sum;
}

去字元中前後的空格的js函數
複製代碼 代碼如下:

function TRIM(value){return value.replace(/^\s*/,'').replace(/\s*$/,''); }

圖片自適應大小函數代碼
複製代碼 代碼如下:

function imgAutoFit(imgObj,maxWidth,maxHeight){
    var heightWidth;
    var widthHeight;
    heightWidth = imgObj.offsetHeight/imgObj.offsetWidth;
    widthHeight = imgObj.offsetWidth/imgObj.offsetHeight;
    if(imgObj.offsetWidth>maxWidth){
        imgObj.width = maxWidth;
        imgObj.height = maxWidth*heightWidth;
    }
    if(imgObj.offsetHeight>maxHeight){
        imgObj.height = maxHeight;
        imgObj.width = maxHeight*widthHeight;
    }
}

function checkTagIsHave(s,t){
    var re=new RegExp("<\\/?"+t+"(?:(?:\\s|\\/)(?:\\n|.)*?)?>","ig");
    var r=re.test(s);
    re=null;
    return r;
}

function imgUploadFit(){
    var u = document.getElementsByName("imgUploadName");
    if(0==u.length) return;
    for(var i=0;i<u.length;i++){
        //imgAutoFit(u[i],560,560);
        imgAutoFit(u[i],320,320);
        u[i].onclick = function(){window.open(this.src);}
        u[i].style.cursor = "pointer";
        u[i].title = "點擊看原圖";
    }
}

function inputContent(theQ,oldContent,re,p){
    var o,oA=["msgframeamend","pingJiaFrame","touSuFrame"];
    for(o in oA){
        if($(oA[o])){
            theQ.editIframe(oA[o]);
            if("msgframeamend"==oA[o]) setTimeout(function(){$(oA[o]).contentWindow.document.body.innerHTML = oldContent.replace(re,"")+p[1];},100);
            return;
        }
    }
}

function checkCommon(n,v,t){
    var s = t?"補充提問":"回複內容";
    var o=document.getElementById(n).contentWindow;
    if("msgframereply" == n){
        if(v == ""){
            alert("請填寫"+s+" !");
            o.focus();
            return false;
        }
    }
    if(""==v.replace(/<\/*?(?:img|p|pre|br)(?:(?:\s|\/)(?:\n|.)*?)?>|\s| /ig,"")&&checkTagIsHave(v,"img")){
        alert("針對圖片,請附上一些說明性的文字!");
        o.focus();
        return false;
    }
    if(10000<LEN(v)){
        alert(s+"超過5000字數限制 !");
        o.focus();
        return false;
    }
    return true;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.