Simple way to check if placeholder is supported?

后端 未结 9 816
执笔经年
执笔经年 2020-12-29 01:00

I want to use the HTML5 \"placeholder\" attribute in my code if the user\'s browser supports it otherwise just print the field name on top of the f

相关标签:
9条回答
  • 2020-12-29 01:30

    http://html5tutorial.info/html5-placeholder.php has the code to do it.

    If you're already using jQuery, you don't really need to do this though. There are placeholder plugins available ( http://plugins.jquery.com/plugin-tags/placeholder ) that will use the HTML5 attribute where possible, and Javascript to simulate it if not.

    0 讨论(0)
  • 2020-12-29 01:32

    Or just:

    if (document.createElement("input").placeholder == undefined) {
        // Placeholder is not supported
    }
    
    0 讨论(0)
  • 2020-12-29 01:32

    If you are using Modernizr, quick catch following:

    if(!Modernizr.input.placeholder){
      ...
    }
    
    0 讨论(0)
  • 2020-12-29 01:34

    I'm trying to do the same... here i wrote this

    if(!('placeholder'in document.createElement("input"))){
       //... document.getElementById("element"). <-- rest of the code
    }}
    

    With this you should have an id to identify the element with the placeholder... I don't know thought if this also help you to identify the element ONLY when the placeholder isn't supported.

    0 讨论(0)
  • 2020-12-29 01:37

    A bit late to the party, but if you're using jQuery or AngularJS you can simplify the method suggested above without using any plugins.

    jQuery

    typeof $('<input>')[0].placeholder == 'string'
    

    AngularJS

    typeof angular.element('<input>')[0].placeholder == 'string'
    

    The checks are very similar, as AngularJS runs jQlite under the hood.

    0 讨论(0)
  • 2020-12-29 01:38

    Hi there this is an old question but hopefully this helps someone.

    This script will check the compatibility of placeholders in your browser, and if its not compatible it will make all input fields with a placeholder use the value="" field instead. Note when the form is submitted it will also change your input back to "" if nothing was entered.

    // Add support for placeholders in all browsers
    var testInput = document.createElement('input');
    testPlaceholderCompatibility = ('placeholder' in testInput);
    if (testPlaceholderCompatibility === false)
    {
       $('[placeholder]').load(function(){
            var input = $(this);
            if (input.val() == '')
            {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        });
    
        $('[placeholder]').focus(function() {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function() {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur().parents('form').submit(function() {
            $(this).find('[placeholder]').each(function() {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                }
            })
        });
    }
    
    0 讨论(0)
提交回复
热议问题