HTML表单“占位”不是在IE 8的工作(HTML Form “placeholder” not w

2019-09-18 23:30发布

您好我已经建立在我的网站上填写表单。 我使用的placeholder="Full Name" ,但在IE8(可能其他版本的IE)浏览任何操作时显示的形式向上

我已经尝试使用value="" onfocus="if (this.value == 'Full Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Full Name';}" ,但形式仍然是空的。 出于某种原因,“全名”显示,当您单击窗体,然后关闭它。

该网站我的工作是[usspcatalystcentre.org.uk] [1],你可以看到我的意思。 前两种形式(标题及全名)是我已经尝试使用

value="" onfocus="if (this.value == 'Full Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Full Name';}" 


该表格的其余部分在哪里我刚使用placeholder

这里是我工作的实际代码

<form action="contact.php" method="post">


                <input id=title name=title type=text value="" onfocus="if(this.value=='Username') this.value='';" onblur="if(this.value=='') this.value='Username';" required class="top-formfields" value='<?php print $_SESSION['title']?>'>  <br /><br />
                <input id=fullname name=fullname type=text value="" onfocus="if(this.value=='Full Name') this.value='';" onblur="if(this.value=='') this.value='Full Name';" required  class="top-formfields" value='<?php print $_SESSION['fullname']?>'> <br /><br />
                <input id=companyname name=companyname type=text placeholder="Company Name" required  class="top-formfields" value='<?php print $_SESSION['companyname']?>'>    <br /><br />
                <input id=companynumber name=companynumber type=text placeholder="Company Registered Number" required  class="top-formfields" value='<?php print $_SESSION['companynumber']?>'> <br /><br />
                <textarea id=address name=address rows=5 placeholder="Address" required class="top-textarea"><?php print $_SESSION['address']?></textarea> <br /><br />
                <input id=postcode name=postcode type=text placeholder="Post Code" required  class="top-formfields" value='<?php print $_SESSION['postcode']?>'>    <br /><br />
                <input id=phonenumber name=phonenumber type=text placeholder="Phone Number" required  class="top-formfields" value='<?php print $_SESSION['phonenumber']?>'>    <br /><br />
                <input id=email name=email type=text placeholder="Email" required  class="top-formfields" value='<?php print $_SESSION['email']?>'> <br /><br />
                <input id=mobile name=mobile type=text placeholder="Mobile" required  class="top-formfields" value='<?php print $_SESSION['mobile']?>'> <br /><br />
                <input id=website name=website type=text placeholder="Website URL" required  class="top-formfields" value='<?php print $_SESSION['website']?>'> <br /><br />

在此先感谢,汤姆

Answer 1:

placeholder属性是HTML5新的提高。 它不支持老年IE-S - http://diveintohtml5.info/forms.html

要模仿它的跨浏览器,你可能想使用jQuery - 例如



Answer 2:

IE 9不支持占位符,这样上来在我脑海里的第一个解决方案是这样的。

祝好运!

<script type="text/javascript"> 
    if(navigator.userAgent.indexOf("MSIE") > 0 )
    {
        $(function()
        {
            var input = document.createElement("input");
            if(('placeholder' in input) == false)
            {
                $('[placeholder]').focus(function()
                {
                    var i = $(this);
                    if(i.val() == i.attr('placeholder'))
                    {
                        i.val('').removeClass('placeholder');
                        if(i.hasClass('password'))
                        {
                            i.removeClass('password'); this.type='password';
                        }
                    }
                }).blur(function()
                {
                    var i = $(this);
                    if(i.val() == '' || i.val() == i.attr('placeholder'))
                    {
                        if(this.type=='password')
                        {
                            i.addClass('password'); this.type='text';
                        }
                        i.addClass('placeholder').val(i.attr('placeholder'));
                    }
                }).blur().parents('form').submit(function()
                {
                    $(this).find('[placeholder]').each(function()
                    {
                        var i = $(this);
                        if(i.val() == i.attr('placeholder')) i.val('');
                    });
                });
            }
        });
    }


    </script>


Answer 3:

汤姆,占位符是不是IE8支持的属性。

我认为这将有助于你与你要去的方向:

与“全名”预填充字段。 给它值=“全名”提前为默认。

onfocus= "if (this.value=='Full Name'){this.select();this.value='';this.style.color='#999999';}else{this.style.color='#000000';}"  

onclick= "if (this.value=='Full Name'){this.select();this.value='';this.style.color='#999999';}else{this.style.color='#000000';}" 

onblur=  "if (this.value==''||this.value=='Full Name'){this.value='Full Name';this.style.color='#999999';}else{this.style.color='#000000';}" 

onkeyup= "if (this.value=='Full Name'){this.select();this.style.color='#999999';}else{this.style.color='#000000';}" 

onchange="if (this.value=='Full Name'){this.style.color='#999999';}else{this.style.color='#000000';}"


Answer 4:

if (!('placeholder' in $('<input>')[0])) { // Create a dummy element for feature detection
    $('input[placeholder], textarea[placeholder]').blur(add).focus(remove).each(add); // Select the elements that have a placeholder attribute
    $('form').submit(function(){$(this).find('input[placeholder], textarea[placeholder]').each(remove);}); // Remove the placeholder text before the form is submitted
}


Answer 5:

将下面的代码作为.js ,并确保更改输入型

$(document).ready(function () {
    if ($.browser.msie) { //this is for only ie condition ( microsoft internet explore )
        $('input[type="text"][placeholder], textarea[placeholder]').each(function () {
            var obj = $(this);
            if (obj.attr('placeholder') != '') {
                obj.addClass('IePlaceHolder');

                if ($.trim(obj.val()) == '' && obj.attr('type') != 'password') {
                    obj.val(obj.attr('placeholder'));
                }
            }
        });

        $('.IePlaceHolder').focus(function () {
            var obj = $(this);
            if (obj.val() == obj.attr('placeholder')) {
                obj.val('');
            }
        });

        $('.IePlaceHolder').blur(function () {
            var obj = $(this);
            if ($.trim(obj.val()) == '') {
                obj.val(obj.attr('placeholder'));
            }
        });
    }
});

http://jsfiddle.net/wbcTm/79/



Answer 6:

确保添加浏览器检测,如果你使用jQuery 1.9+。 你可以从你的终端使用此命令安装NPM安装jquery.browser



文章来源: HTML Form “placeholder” not working in IE 8