實現ie瀏覽器對placeholder的兼容

/**
 * 函數:實現ie瀏覽器對placeholder的兼容
 * 輸入:
 * 輸出:
**/
function compatiblePlaceholder(){
    var isPlaceholder = 'placeholder' in document.createElement('input');
    if (!isPlaceholder) {
        $("input[placeholder]").each(function() {
            var text = $(this).attr("placeholder");
            var $span = $('<span class="place_holder">'+ text +'</span>');
            $(this).before($span);
            var $that = $(this);
            $span.on("click focus",function() {
                $that.click();
            });
            $(this).click(function() {
                $(this).focus();
                $span.hide();
            });
            $(this).blur(function(){
                $span.show();
            });
        });
        $(".two_ph").each(function() {
            $(this).find(".place_holder").eq(1).addClass("for_ben_rate");
        });
    }
}

老版本ie瀏覽器不支持placeholder功能,爲了實現該功能,有多個思路,以上是本人提供的一個思路,利用一個span標籤來模擬placeholder。

發佈了45 篇原創文章 · 獲贊 12 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章