繁体   English   中英

占位符不在IE10中工作

[英]Placeholder not working in IE10

的HTML:

<div class="div-input">
    <input type="text" id="loginname" name="username" value="" maxlength="25" 
        class="form-input" placeholder="Username"
    />
</div>
<div class="div-input">
    <input type="password" id="loginpassword" name="password" maxlength="25" 
        class="form-input" placeholder="Password"
    />
</div>
<div>
    <input type="submit" name="login" value="LOGIN" class="form-button" 
        onclick="return check_login(&quot;/&quot;)" 
    />
    <span id="logInfo"></span><span style="display: none;" id="overlay"></span>
    <span style="display: none;" id="popup">
        <img src="/public/images/loading.gif" />
    </span>
</div>

我的问题是,占位符在所有浏览器中都能正常工作,对于IE8和IE9,我使用Javascript解决了该问题,但在IE10中,占位符正在运行,但工作方式不正确。

  1. 发生的结果是,在页面加载时,如果Placeholder是占位符,则在IE10中我只能得到Placeholde作为占位符(最后一个字母消失),但是如果我单击输入框,并且在页面外部,它将显示正确的占位符作为Placeholder

  2. 如果在输入字段中输入任何单词,则输入字段的角落会出现一个十字符号(关闭符号),仅在IE10中会发生这种情况。

听起来您的JavaScript破坏了本机IE10占位符功能。

要对此进行测试并希望对其进行修复,请将您为占位符编写的javascript封装在条件语句中,以使其仅适用于IE9及以下版本。

<!--[if lt IE 9]>
<script>
  document.createElement('header');
  document.createElement('nav');
</script>
<![endif]-->

另外,还有一个由Mathias Bynens提供的非常不错的jquery插件,可以免费使用,并为您处理所有这些操作: https : //github.com/mathiasbynens/jquery-placeholder

一些CMS已使用此代码构建了一个插件。 Wordpress插件在这里: http : //wordpress.org/plugins/html5-placeholder-polyfill/

我不确定在ie10中是否支持输入支持的placeHolder属性,但是如果您要在ie中使用占位符,则可以像下面这样通过jquery进行此操作。

if(navigator.appVersion.match(/MSIE [\d.]+/)){
    var placeholderText = 'Some Placeholder Text';
    $('#loginname').val(placeholderText);
    $('#loginname').blur(function(){
        $(this).val() == '' ? $(this).val(placeholderText) : false;
    });
    $('#loginname').focus(function(){
        $(this).val() == placeholderText ? $(this).val('') : false;
    });
}

您也可以为密码做同样的事情。

您是否使用代码尝试过这种方式...我认为应该可以...

$('#loginname').attr('placeholder', 'username');
$('#password').attr('placeholder', 'password');

尝试此操作,是浏览器中的jquery插件不完全支持占位符。 留下您的php代码并添加此js应该可以正常工作:

(function($) {

/**
 * Spoofs placeholders in browsers that don't support them (eg Firefox 3)
 * 
 * Copyright 2011 Dan Bentley
 * Licensed under the Apache License 2.0
 *
 * Author: Dan Bentley [github.com/danbentley]
 */

// Return if native support is available.
if ("placeholder" in document.createElement("input")) return;

$(document).ready(function(){
    $(':input[placeholder]').not(':password').each(function() {
        setupPlaceholder($(this));
    });

    $(':password[placeholder]').each(function() {
        setupPasswords($(this));
    });

    $('form').submit(function(e) {
        clearPlaceholdersBeforeSubmit($(this));
    });
});

function setupPlaceholder(input) {

    var placeholderText = input.attr('placeholder');

    setPlaceholderOrFlagChanged(input, placeholderText);
    input.focus(function(e) {
        if (input.data('changed') === true) return;
        if (input.val() === placeholderText) input.val('');
    }).blur(function(e) {
        if (input.val() === '') input.val(placeholderText); 
    }).change(function(e) {
        input.data('changed', input.val() !== '');
    });
}

function setPlaceholderOrFlagChanged(input, text) {
    (input.val() === '') ? input.val(text) : input.data('changed', true);
}

function setupPasswords(input) {
    var passwordPlaceholder = createPasswordPlaceholder(input);
    input.after(passwordPlaceholder);

    (input.val() === '') ? input.hide() : passwordPlaceholder.hide();

    $(input).blur(function(e) {
        if (input.val() !== '') return;
        input.hide();
        passwordPlaceholder.show();
    });

    $(passwordPlaceholder).focus(function(e) {
        input.show().focus();
        passwordPlaceholder.hide();
    });
}

function createPasswordPlaceholder(input) {
    return $('<input>').attr({
        placeholder: input.attr('placeholder'),
        value: input.attr('placeholder'),
        id: input.attr('id'),
        readonly: true
    }).addClass(input.attr('class'));
}

function clearPlaceholdersBeforeSubmit(form) {
    form.find(':input[placeholder]').each(function() {
        if ($(this).data('changed') === true) return;
        if ($(this).val() === $(this).attr('placeholder')) $(this).val('');
    });
}
})(jQuery);

然后,您只需通过以下方式在php页面或另一个js中调用插件:

 $(function() {
    // Invoke the plugin
    $('input, textarea').placeholder();       
   });

希望这可以帮助!

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM