码迷,mamicode.com
首页 > Web开发 > 详细

Jquery简单的placeholder效果

时间:2014-06-02 14:00:10      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

Jquery简单的placeholder效果

  由于IE6-IE9不支持HTML5中的placeholder,所以自己依赖于Jquery简单的写了一个,供参考!

  先看看效果吧!如下JSFiddle地址

 查看效果链接

JS代码如下:

bubuko.com,布布扣
/*
 * JS placeholder
 * IE6-IE9不支持HTML5中的placeholder
 */
    
function Placeholder(options) {
    this.config = {
        defaultColor: ‘#ccc‘,
        curColor: ‘#333‘,
        targetElem: ‘.placeholderCls‘
    };

    this.init(options);
}

Placeholder.prototype = {

    constructor:Placeholder,

    init: function(options){
        this.config = $.extend(this.config, options || {});
        var self = this,
            _config = self.config;
        
        $(_config.targetElem).each(function(){
            var supportPlaceholder = ‘placeholder‘ in document.createElement(‘input‘);
            if(!supportPlaceholder) {
                var defaultValue = $(this).attr(‘placeholder‘);
                $(this).focus(function(){
                    var pattern = new RegExp("^" + defaultValue + "$|^$");
                    pattern.test($(this).val()) && $(this).val(‘‘).css(‘color‘,_config.curColor);
                }).blur(function(){
                    if($(this).val() == defaultValue) {
                        $(this).css(‘color‘,_config.defaultColor);
                    }else if($(this).val().length == 0){
                        $(this).val(defaultValue).css(‘color‘, _config.defaultColor);
                    }
                }).trigger(‘blur‘);
            }
        });
        
    }    
};
    
bubuko.com,布布扣

HTML代码如下:

bubuko.com,布布扣
<form id="form1">
    <h3>通过value方式模拟placeholder</h3>
        
    <p>
        <label for="username1">用户名:</label>
        <input type="text" class="placeholderCls" placeholder="请输入用户名">
    </p>
    <p>
        <label for="address1">地 址:</label>
        <input type="text" class="placeholderCls" placeholder="请输入地址">
    </p>
    <p>
        <label for="remarks1">备 注:</label>
        <textarea placeholder="请输入备注" class="placeholderCls"></textarea>
    </p>
</form>
bubuko.com,布布扣

代码初始化:

<script>
    new Placeholder({});
</script>

 

 

 

Jquery简单的placeholder效果,布布扣,bubuko.com

Jquery简单的placeholder效果

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/tugenhua0707/p/3763075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!