placeholder IE兼容问题

本文介绍了HTML5中新加入的placeholder属性,该属性允许在文本框中显示提示信息,当用户开始输入时,提示信息会自动消失。此外,还提供了一个在不支持此属性的浏览器(如IE)中使用jQuery模拟实现的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTML5里新引入很多有趣的新特征 ,文本框(INPUT)里的placeholder属性。placeholder属性能够让你在文本框里显示提示信息,一旦你在文本框里输入了什么信息,提示信息就会隐藏。你以前可能无数次看到这种效果,但那些大部分是用JavaScript里实现的,而现在,HTML5提供了原生支持,而且效果更好!

<form action="/example/html5/demo_form.asp" method="get">
<input type="search" name="user_search" placeholder="Search W3School" />
<input type="submit" />
</form>

鉴于 placeholder 是个 html5 的属性, IE 下的 placeholder 模拟实现

$(function(){
    // placeholder
    if ($.browser.msie) {
        $(':text,:password').each(function(){
            var label = $('<label>');
            label.css({
                'position' : 'absolute',
                'margin-left' : '5px',
                'margin-top' : '5px',
                'color' : 'gray',
                'z-index' : 1
            }).text($(this).attr('placeholder')).click(function(){
                label.hide().next(':input').focus();
            });
            $(this).before(label).focus(function(){
                label.hide();
            }).blur(function(){
                if ($(this).val() == '') {
                    label.show();
                }
            });
        });
    }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值