亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

JavaScript常用腳本集錦6

Acceml / 1355人閱讀

摘要:它會指出一個類是繼承自另一個類的。測試測試代碼來源頁面倒計時的一段運用倒計時的一段腳本。截止日期符合日期格式,比如等有效日期。截止的天數(shù)小時分鐘秒數(shù)組成的對象。

清楚節(jié)點內(nèi)的空格
function cleanWhitespace(element) {
    //如果不提供參數(shù),則處理整個HTML文檔
    element = element || document;
    //使用第一個節(jié)點作為開始指針
    var cur = element.firstChild;
    //一直循環(huán),直到?jīng)]有子節(jié)點為止。
    while (cur != null) {
        //如果節(jié)點是文本節(jié)點,并且只包含空格
        if ((cur.nodeType == 3) && !/S/.test(cur.nodeValue)) {
            element.removeChild(cur);
        }
        //一個節(jié)點元素
        else if (cur.nodeType == 1) {
            //遞歸整個文檔
            cleanWhitespace(cur);
        }
        cur = cur.nextSibling;  //遍歷子節(jié)點
    }
}

代碼來源:https://gist.github.com/hehongwei44/9105deee7b9bde88463b

JavaScript中的類繼承實現(xiàn)方式
/**
 * 把一個實例方法添加到一個類中
 * 這個將會添加一個公共方法到 Function.prototype中,
 * 這樣通過類擴展所有的函數(shù)都可以用它了。它要一個名稱和一個函數(shù)作為參數(shù)。
 * 它返回 this。當(dāng)我寫一個沒有返回值的方法時,我通常都會讓它返回this。
 * 這樣可以形成鏈?zhǔn)秸Z句。
 *
 * */
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
/**
 * 它會指出一個類是繼承自另一個類的。
 * 它必須在兩個類都定義完了之后才能定義,但要在方法繼承之前調(diào)用。
 *
 * */
Function.method("inherits", function (parent) {
    var d = 0, p = (this.prototype = new parent());

    this.method("uber", function uber(name) {
        var f, r, t = d, v = parent.prototype;
        if (t) {
            while (t) {
                v = v.constructor.prototype;
                t -= 1;
            }
            f = v[name];
        } else {
            f = p[name];
            if (f == this[name]) {
                f = v[name];
            }
        }
        d += 1;
        r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
        d -= 1;
        return r;
    });
    return this;
});
/**
 *
 * The swiss方法對每個參數(shù)進行循環(huán)。每個名稱,
 * 它都將parent的原型中的成員復(fù)制下來到新的類的prototype中
 *
 * */
Function.method("swiss", function (parent) {
    for (var i = 1; i < arguments.length; i += 1) {
        var name = arguments[i];
        this.prototype[name] = parent.prototype[name];
    }
    return this;
});

代碼來源:https://gist.github.com/hehongwei44/2f89e61a0e6d4fd722c4

將單個字符串的首字母大寫
/**
*
* 將單個字符串的首字母大寫
* 
*/
var fistLetterUpper = function(str) {
        return str.charAt(0).toUpperCase()+str.slice(1);
};
console.log(fistLetterUpper("hello"));      //Hello
console.log(fistLetterUpper("good"));       //Good

代碼來源:https://gist.github.com/hehongwei44/7e879f795226316c260d

變量的類型檢查方式
/**
*
* js的類型檢測方式->typeof、constuctor。
* 推薦通過構(gòu)造函數(shù)來檢測變量的類型。
*/
var obj = {key:"value"},
        arr = ["hello","javascript"],
        fn  = function(){},
        str = "hello js",
        num = 55,
        bool = true,
        User = function(){},
        user = new User();
    /*typeof測試*/
    console.log(typeof obj);    //obj
    console.log(typeof arr);    //obj
    console.log(typeof fn);     //function
    console.log(typeof str);    //string
    console.log(typeof num);    //number
    console.log(typeof bool);   //boolean
    console.log(typeof user);   //object
    /*constructor測試*/
    console.log(obj.constructor == Object); //true
    console.log(arr.constructor == Array);  //true
    console.log(str.constructor == String); //true
    console.log(num.constructor == Number); //true
    console.log(bool.constructor == Boolean);//true
    console.log(user.constructor == User);  //true

代碼來源:https://gist.github.com/hehongwei44/1d808ca9b7c67745f689

頁面倒計時的一段運用
/**
*
* @descition: 倒計時的一段腳本。
* @param:deadline ->截止日期 符合日期格式,比如2012-2-1 2012/2/1等有效日期。
* @return -> 截止的天數(shù)、小時、分鐘、秒數(shù)組成的object對象。
*/
function getCountDown(deadline) {
        var activeDateObj = {},
             currentDate  = new Date().getTime(),            //獲取當(dāng)前的時間
             finalDate    = new Date(deadline).getTime(),    //獲取截止日期
             intervaltime = finalDate - currentDate;         //有效期時間戳

        /*截止日期到期的話,則不執(zhí)行下面的邏輯*/
        if(intervaltime < 0) {
            return;
        }

        var totalSecond = ~~(intervaltime / 1000),     //得到秒數(shù)
            toDay       = ~~(totalSecond / 86400 ),   //得到天數(shù)
        toHour      = ~~((totalSecond -  toDay * 86400) / 3600), //得到小時
        tominute    = ~~((totalSecond -  toDay * 86400 - toHour * 3600) / 60), //得到分?jǐn)?shù)
        toSeconde   = ~~(totalSecond - toDay * 86400 - toHour * 3600 -tominute * 60);

    /*裝配obj*/
    activeDateObj.day    = toDay;
    activeDateObj.hour   = toHour;
    activeDateObj.minute = tominute;
    activeDateObj.second = toSeconde;

    return activeDateObj;
}

代碼來源:https://gist.github.com/hehongwei44/a1205e9ff17cfc2359ca

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/87691.html

相關(guān)文章

  • JavaScript常用腳本集錦2

    摘要:把中的偽數(shù)組轉(zhuǎn)換為真數(shù)組在中,函數(shù)中的隱藏變量和用獲得的元素集合都不是真正的數(shù)組,不能使用等方法,在有這種需要的時候只能先轉(zhuǎn)換為真正的數(shù)組。檢測元素是否支持某個屬性代碼用法創(chuàng)建和使用命名空間使用方式 把JavaScript中的偽數(shù)組轉(zhuǎn)換為真數(shù)組 在 JavaScript 中, 函數(shù)中的隱藏變量 arguments 和用 getElementsByTagName 獲得的元素集合(Nod...

    xialong 評論0 收藏0
  • JavaScript常用腳本集錦5

    摘要:代碼來源一些常用的操作方法介紹查找相關(guān)元素的前一個兄弟元素的方法。查找元素指定層級的父元素。 DOM操作的增強版功能函數(shù) /** * 將一個DOM節(jié)點、HTML字符串混合型參數(shù) * 轉(zhuǎn)化為原生的DOM節(jié)點數(shù)組 * * */ function checkElem(a) { var r = []; if (a.constructor != Array) { ...

    joywek 評論0 收藏0
  • JavaScript常用腳本集錦7

    摘要:將加法和加上校驗位能被整除。下面分別分析出生日期和校驗位檢查生日日期是否正確輸入的身份證號里出生日期不對將位身份證轉(zhuǎn)成位校驗位按照的規(guī)定生成,可以認(rèn)為是數(shù)字。校驗位按照的規(guī)定生成,可以認(rèn)為是數(shù)字。表示全部為中文為不全是中文,或沒有中文。 判斷是否是合理的銀行卡卡號 //Description: 銀行卡號Luhm校驗 //Luhm校驗規(guī)則:16位銀行卡號(19位通用): // 1.將...

    lindroid 評論0 收藏0
  • JavaScript常用腳本集錦1

    摘要:初始化參數(shù)可選參數(shù),必填參數(shù)可選,只有在請求時需要參數(shù)可選回調(diào)函數(shù)可選參數(shù)可選,默認(rèn)為參數(shù)可選,默認(rèn)為創(chuàng)建引擎對象打開發(fā)送普通文本接收文檔將字符串轉(zhuǎn)換為對象最后,說明一下此函數(shù)的用法。即等待與成功回調(diào),后標(biāo)志位置為。 jquery限制文本框只能輸入數(shù)字 jquery限制文本框只能輸入數(shù)字,兼容IE、chrome、FF(表現(xiàn)效果不一樣),示例代碼如下: $(input).keyup(...

    ygyooo 評論0 收藏0
  • JavaScript常用腳本集錦3

    通過數(shù)組,拓展字符串拼接容易導(dǎo)致性能的問題 function StringBuffer() { this.__strings__ = new Array(); } StringBuffer.prototype.append = function (str) { this.__strings__.push(str); return this; } StringBuffer....

    dack 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<