简体   繁体   English

替换占位符文本的功能

[英]Function to replace placeholder text

We've created a function to work as a substitute for placeholder text in fields, that should run if the browser hasn't support for the placeholder attribute (ie early IE browsers). 我们创建了一个函数来替代字段中的占位符文本,如果浏览器不支持占位符属性(即,早期的IE浏览器),则该函数应运行。

However, it doesn't seem to work and we can't figure out the reason. 但是,它似乎不起作用,我们也无法找出原因。 Could you see what's wrong? 你能知道怎么了吗?

setup_placeholders = (function() {
    $.support.placeholder = false;
    test = document.createElement('input');
    if('placeholder' in test) {
        $.support.placeholder = true;
        return function() {}
    } else {
        return function(){
            $(function() {
                var active = document.activeElement;
                $('form').delegate(':text', 'focus', function () {
                    var _placeholder = $(this).attr('placeholder'),
                        _val = $(this).val();
                    if (_placeholder != '' && _val == _placeholder) {
                        $(this).val('').removeClass('hasPlaceholder');
                    }
                }).delegate(':text', 'blur', function () {
                    var _placeholder = $(this).attr('placeholder'),
                        _val = $(this).val();

                    if (!_placeholder && ( _val == '' || _val == _placeholder)) {
                        $(this).val(_placeholder).addClass('hasPlaceholder');
                    }
                }).submit(function () {
                    $(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
                });
                $(':text').blur();
                $(active).focus();
            });
        }
    }
})();

JQuery approach : jQuery方法:

(function($) {
    var safe = [];
    $.fn.store = function() {
        safe[$(this).attr('id')] = $(this).val();
        $(this).click(function() {if ($(this).val() == safe[$(this).attr('id')]) $(this).val('');});
        $(this).blur(function() {if ($(this).val() == '') $(this).val(safe[$(this).attr('id')]);});
    };
}( jQuery ));

Usage : 用法:

<input type="text" id="txt_field" name="txt_field" value="Placeholder">
<script>
    $('#txt_field').store();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用数据库中的文本替换占位符 - Replace a placeholder with text from database 用变量值递归替换占位符文本 - Recursively replace placeholder text with the value of a variable 试图用实际数据替换占位符文本,但它不起作用 - Trying to replace placeholder text with actual data, but it is not working 如何用存储在本地存储中的文本替换 HTML 中的替换 {{content}} 占位符 - How to replace Replace {{content}} placeholder in HTML with text stored in local storage 有没有办法可以将占位符文本放入文本输入中,并在用户键入时将占位符文本的字符 1 替换为 1? - Is there a way I can put placeholder text in a textinput and replace the characters of the placeholder text 1 by 1 as the user is typing? 使用javascript替换表单占位符文本 - 表单不在页面上 - Replace form placeholder text using javascript - form not on page 使用占位符数组替换文本:JavaScript中的替换对 - Replace text using an array of placeholder : replacement pairs in JavaScript 使用javascript将html占位符文本替换为html元素 - Replace html placeholder text with html element using javascript 需要输入URL模板占位符查找和替换功能 - Need input for URL Template placeholder find and replace function “简单”文本替换功能 - “Simple” Text replace function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM