Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JQUERY 插件:placeholder(转自个人百度空间) #36

Open
mishe opened this issue Dec 3, 2015 · 0 comments
Open

JQUERY 插件:placeholder(转自个人百度空间) #36

mishe opened this issue Dec 3, 2015 · 0 comments

Comments

@mishe
Copy link
Owner

mishe commented Dec 3, 2015

$.fn.extend({
        placeholder:function () {
            if ("placeholder" in document.createElement("input")) {
                return this //如果原生支持placeholder属性,则返回对象本身
            } else {
                return this.each(function () {
                    var _this = $(this);
                    if(_this.attr('type')!='password'){
                        _this.val(_this.attr("placeholder")).focus(function () {
                            if (_this.val() === _this.attr("placeholder")) {
                                _this.val("")
                            }
                        }).blur(function () {
                                if (_this.val().length === 0) {
                                    _this.val(_this.attr("placeholder"))
                                }
                            });
                    }else{
                        var newObj=$('<input type="text" value="'+_this.attr('placeholder')+'" autocomplete="off" class="'+_this[0].className+'" />');
                        _this.hide().after(newObj);
                        newObj.focus(function(){
                            newObj.hide();
                            _this.show().focus();
                        });
                        _this.blur(function(){
                            if(_this.val() == '') {
                                newObj.show();
                                _this.hide();
                            }
                        });
                    }
                })
            }
        }
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant