joken-前端工程师

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::
function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

function placeholderIe() {
    function isPlaceholder() {
        var input = document.createElement('input');
        return 'placeholder' in input;
    }
    if (!isPlaceholder()) { //不支持placeholder 用jquery来完成
        $(document).ready(function() {
            if (!isPlaceholder()) {
                $("input,textarea").each( 
                    function() {
                        if ($(this).val() == "" && $(this).attr("placeholder") != null) {
                            var self = this;
                            var onlyId = guid();
                            $(self).attr("data-pid", onlyId)

                            function gen() {
                                var x = $(self)[0].offsetLeft;
                                var y = $(self)[0].offsetTop;
                                var h = $(self).height();
                                var left = parseInt(x) + 10;
                                var html = '<span id="' + onlyId + '" class="holder-span" style="left:' + left + 'px;top:' + y + 'px;line-height:' + h + 'px">' + $(self).attr("placeholder") + '</span>';
                                $(self).parent().append($(html))
                            }
                            gen()
                            $(this).focus(function() {
                                $("#" + onlyId + "").hide()
                            });
                            $(this).blur(function() {
                                if ($(this).val() == "" && $(this).attr("placeholder") != "") {
                                    $("#" + onlyId + "").show()
                                }
                            });
                            $("#" + onlyId + "").click(function() {
                                $(this).hide()
                                if ($(self).attr("data-pid") == onlyId) {
                                    $(self).focus()
                                }
                            });
                        }
                    });
            }
        });
    }
}

 

posted on 2018-07-23 14:21  joken1310  阅读(223)  评论(0编辑  收藏  举报