侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

js input placeholder自动变值

2024-05-08 星期三 / 0 评论 / 0 点赞 / 59 阅读 / 4086 字

(function ($) { $.fn.__Placeholder = function (options) { this.el = $(this); this.options =

(function ($) {  
    $.fn.__Placeholder = function (options) {
        this.el = $(this);
        this.options = options;
        this.fade_i = 0;
        this.defualtsArray = ['fancy-hummingbird','amusing-meerkat','efficient-manatee','complete-otter','delicious-goat','mammoth-toad','mellow-spider','profuse-hummingbird','near-otter','delicious-eagle'];
        //初始化方法  
        this._init = function(){
            this._start();
        }
        this._start = function (){
            var _this = this;
            _this._placeholder = _this.el.attr('placeholder');
            if(!_this._placeholder){
                _this._placeholder = _this.defualtsArray[this._getRandom(_this.defualtsArray.length-1)];
                _this._showPlaceholder(_this._placeholder);
            }else{
                _this._placeholder = _this.defualtsArray[_this._getRandom(_this.defualtsArray.length-1)];
                setTimeout(function (){
                    _this._showPlaceholder(_this._placeholder);
                },5000);
            }
        }
        this._showPlaceholder = function (_content){
            var _this = this,
                contentLength = _content.length;
            if(_this.fade_i < contentLength){
                _this.el.attr('placeholder',_content.substring(0,_this.fade_i));
                _this.fade_i = _this.fade_i + 1;
                setTimeout(function (){
                    _this._showPlaceholder(_content);
                },35);
            }else{
                _this.fade_i = 0;
                _this._start();
            }
        }
        this._getRandom = function (n){
            return Math.floor(Math.random()*n+1);
        }
        this.el.on('input', function(){
            //输入
        });
        this.getValue = function(){
            var value = this.el.val();
            if(value == ''){
                value = this.el.attr('placeholder');
            }
            console.log(value);
        }
        this._init();
        return this;  
    };  
})(jQuery); 

广告 广告

评论区