【回忆1314】回忆之placeholder

直接看效果点这里

HTML

<!DOCTYPE html>
<html>
<head lang="zh-CN">
    <meta charset="utf-8">
    <title> Placeholder </title>
</head>
<body>
<input class="J_Placeholder" type="text" value=""/>
<textarea class="J_Placeholder"></textarea>
<script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<script src="jquery.placeholder.js"></script>
<script>
$(‘.J_Placeholder‘).placeholder({
    ‘txt‘: ‘请输入提示信息‘
});
</script>
</body>
</html>

JS

/**
 * @description: 表单input、textarea占位提示
 * @author:[email protected]
 * @update: 2014/11/10
 *
 * @param txt{string} 提示信息语句
 *
 * 用法
 *
 * $(‘.J_Placeholder‘).placeholder({
 *     ‘txt‘: ‘请输入提示信息‘
 * });
 */

;(function($){

    var methods = {
        init: function(opts){
            this.isHtml5Placeholder() ? this.changeHtml5Placeholder(opts) : this.changePlaceholder(opts);
        },
        isHtml5Placeholder: function(){
            var res = ‘placeholder‘ in document.createElement(‘input‘);
            return res;
        },
        changePlaceholder: function(opts){
            opts.that.attr(‘value‘) == ‘‘ && opts.that.attr({
                ‘value‘: opts.txt
            });

            opts.that
                .on(‘focus‘, function(){
                    if($(this).val() === opts.txt){
                        $(this).attr(‘value‘, ‘‘);
                    }
                })
                .on(‘blur‘, function(){
                    if($(this).val() == ‘‘){
                        $(this).attr({
                            ‘value‘: opts.txt
                        });
                    }
                });
        },
        changeHtml5Placeholder: function(opts){
            opts.that.attr({
                ‘placeholder‘: opts.txt
            });
        }
    };

    $.fn.placeholder = function(opts){
        opts = $.extend({
            ‘that‘: this,
            ‘txt‘: ‘‘
        }, opts || {});
        methods.init(opts);
        return this;
    };

})(jQuery);
时间: 2024-08-30 11:48:51

【回忆1314】回忆之placeholder的相关文章

【回忆1314】回忆之浮窗

直接看效果点这里 HTML <!DOCTYPE html> <html> <head lang="zh-CN"> <meta charset="utf-8"> <title> 浮窗 </title> <style> * { margin: 0; padding: 0; } .staff { width: 1200px; margin: 0 auto; height: 800px; l

【回忆1314】回忆之星星评分

直接看效果点这里 HTML <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title> 星星评分 </title> <link rel="stylesheet" href="star.css"/> </head> <body> <ul

【回忆1314】抽奖之大富翁

直接看效果点这里 <!doctype html> <html> <head> <meta charset="UTF-8"> <title> 大富翁 </title> <style> ul {list-style: none; } .lottery { position: relative; width: 184px; height: 184px; line-height: 58px; text-alig

【回忆1314】回忆之日历

直接看效果点这里 HTML <!DOCTYPE html> <html> <head lang="zh-CN"> <meta charset="utf-8"> <title> 简易多栏日历 </title> <link rel="stylesheet" href="calendar.css"> </head> <body>

【回忆1314】回忆之横竖滑动门

直接看效果点这里 HTML <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title> 横竖滑动门 </title> <link rel="stylesheet" href="sliding-door.css"/> </head> <body&g

【回忆1314】回忆之城市搜索

直接看效果点这里 HTML <!DOCTYPE html> <html> <head lang="zh-CN"> <meta charset="utf-8"> <title> 城市搜索 </title> <link rel="stylesheet" href="ui-departure.css"> </head> <body&

【回忆1314】回忆之分页

直接看效果点这里 HTML <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>分页插件</title> <link rel="stylesheet" href="page.min.css"/> </head> <body> <

【回忆1314】回忆之目的地搜索

直接看效果点这里 HTML <!DOCTYPE html> <html> <head lang="zh-CN"> <meta charset="utf-8"> <title> 目的地搜索 </title> <link rel="stylesheet" href="ui-destination.css"> <style> .city,

【回忆1314】鼠标离开方向检测

直接看效果点这里 <html> <head>鼠标离开方向检测</head> <body> <div id="msg">鼠标放到框上,然后移开.</div> <div id="wrap" style="width:200px; height: 100px; line-height:30px; margin: 100px auto; text-align: center; borde