PageSourceSearch

https://cmp.com.tw/content/js/plugin/jquery.placeholder.js

js cmp.com.tw collected 2026-10-01 14:12:31 UTC 7,592 bytes, 200 lines download raw bytes

1/*! http://mths.be/placeholder v2.1.1 by @mathias */
2(function(factory) {
3    if (typeof define === 'function' && define.amd) {
4        // AMD
5        define(['jquery'], factory);
6    } else if (typeof module === 'object' && module.exports) {
7        factory(require('jquery'));
8    } else {
9        // Browser globals
10        factory(jQuery);
11    }
12}(function($) {
13
14    // Opera Mini v7 doesn't support placeholder although its DOM seems to indicate so
15    var isOperaMini = Object.prototype.toString.call(window.operamini) == '[object OperaMini]';
16    var isInputSupported = 'placeholder' in document.createElement('input') && !isOperaMini;
17    var isTextareaSupported = 'placeholder' in document.createElement('textarea') && !isOperaMini;
18    var valHooks = $.valHooks;
19    var propHooks = $.propHooks;
20    var hooks;
21    var placeholder;
22
23    if (isInputSupported && isTextareaSupported) {
24
25        placeholder = $.fn.placeholder = function() {
26            return this;
27        };
28
29        placeholder.input = placeholder.textarea = true;
30
31    } else {
32
33        var settings = {};
34
35        placeholder = $.fn.placeholder = function(options) {
36
37            var defaults = {customClass: 'placeholder'};
38            settings = $.extend({}, defaults, options);
39
40            var $this = this;
41            $this
42                .filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
43                .not('.'+settings.customClass)
44                .bind({
45                    'focus.placeholder': clearPlaceholder,
46                    'blur.placeholder': setPlaceholder
47                })
48                .data('placeholder-enabled', true)
49                .trigger('blur.placeholder');
50            return $this;
51        };
52
53        placeholder.input = isInputSupported;
54        placeholder.textarea = isTextareaSupported;
55
56        hooks = {
57            'get': function(element) {
58                var $element = $(element);
59
60                var $passwordInput = $element.data('placeholder-password');
61                if ($passwordInput) {
62                    return $passwordInput[0].value;
63                }
64
65                return $element.data('placeholder-enabled') && $element.hasClass(settings.customClass) ? '' : element.value;
66            },
67            'set': function(element, value) {
68                var $element = $(element);
69
70                var $passwordInput = $element.data('placeholder-password');
71                if ($passwordInput) {
72                    return $passwordInput[0].value = value;
73                }
74
75                if (!$element.data('placeholder-enabled')) {
76                    return element.value = value;
77                }
78                if (value === '') {
79                    element.value = value;
80                    // Issue #56: Setting the placeholder causes problems if the element continues to have focus.
81                    if (element != safeActiveElement()) {
82                        // We can't use `triggerHandler` here because of dummy text/password inputs :(
83                        setPlaceholder.call(element);
84                    }
85                } else if ($element.hasClass(settings.customClass)) {
86                    clearPlaceholder.call(element, true, value) || (element.value = value);
87                } else {
88                    element.value = value;
89                }
90                // `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363
91                return $element;
92            }
93        };
94
95        if (!isInputSupported) {
96            valHooks.input = hooks;
97            propHooks.value = hooks;
98        }
99        if (!isTextareaSupported) {
100            valHooks.textarea = hooks;
101            propHooks.value = hooks;
102        }
103
104        $(function() {
105            // Look for forms
106            $(document).delegate('form', 'submit.placeholder', function() {
107                // Clear the placeholder values so they don't get submitted
108                var $inputs = $('.'+settings.customClass, this).each(clearPlaceholder);
109                setTimeout(function() {
110                    $inputs.each(setPlaceholder);
111                }, 10);
112            });
113        });
114
115        // Clear placeholder values upon page reload
116        $(window).bind('beforeunload.placeholder', function() {
117            $('.'+settings.customClass).each(function() {
118                this.value = '';
119            });
120        });
121
122    }
123
124    function args(elem) {
125        // Return an object of element attributes
126        var newAttrs = {};
127        var rinlinejQuery = /^jQuery\d+$/;
128        $.each(elem.attributes, function(i, attr) {
129            if (attr.specified && !rinlinejQuery.test(attr.name)) {
130                newAttrs[attr.name] = attr.value;
131            }
132        });
133        return newAttrs;
134    }
135
136    function clearPlaceholder(event, value) {
137        var input = this;
138        var $input = $(input);
139        if (input.value == $input.attr('placeholder') && $input.hasClass(settings.customClass)) {
140            if ($input.data('placeholder-password')) {
141                $input = $input.hide().nextAll('input[type="password"]:first').show().attr('id', $input.removeAttr('id').data('placeholder-id'));
142                // If `clearPlaceholder` was called from `$.valHooks.input.set`
143                if (event === true) {
144                    return $input[0].value = value;
145                }
146                $input.focus();
147            } else {
148                input.value = '';
149                $input.removeClass(settings.customClass);
150                input == safeActiveElement() && input.select();
151            }
152        }
153    }
154
155    function setPlaceholder() {
156        var $replacement;
157        var input = this;
158        var $input = $(input);
159        var id = this.id;
160        if (input.value === '') {
161            if (input.type === 'password') {
162                if (!$input.data('placeholder-textinput')) {
163                    try {
164                        $replacement = $input.clone().attr({ 'type': 'text' });
165                    } catch(e) {
166                        $replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' }));
167                    }
168                    $replacement
169                        .removeAttr('name')
170                        .data({
171                            'placeholder-password': $input,
172                            'placeholder-id': id
173                        })
174                        .bind('focus.placeholder', clearPlaceholder);
175                    $input
176                        .data({
177                            'placeholder-textinput': $replacement,
178                            'placeholder-id': id
179                        })
180                        .before($replacement);
181                }
182                $input = $input.removeAttr('id').hide().prevAll('input[type="text"]:first').attr('id', id).show();
183                // Note: `$input[0] != input` now!
184            }
185            $input.addClass(settings.customClass);
186            $input[0].value = $input.attr('placeholder');
187        } else {
188            $input.removeClass(settings.customClass);
189        }
190    }
191
192    function safeActiveElement() {
193        // Avoid IE9 `document.activeElement` of death
194        // https://github.com/mathiasbynens/jquery-placeholder/pull/99
195        try {
196            return document.activeElement;
197        } catch (exception) {}
198    }
199
200}));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.