首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Textarea的Internet Explorer占位符

Textarea的Internet Explorer占位符
EN

Stack Overflow用户
提问于 2012-08-01 04:32:21
回答 4查看 7.3K关注 0票数 0

我的应用程序需要一个文本字段和一个文本区域字段的占位符。我知道Internet Explorer不支持占位符。我环顾四周,发现了一些后备代码,它们只对文本字段有效。我怎样才能让它在文本区域中也能工作。

代码:

代码语言:javascript
代码运行次数:0
运行
复制
jQuery(function() {
    jQuery.support.placeholder = false;
    test = document.createElement('input');
    if('placeholder' in test) jQuery.support.placeholder = true;
});

$(function() {
    if(!$.support.placeholder) { 
        var active = document.activeElement;
        $('input[type=text], textarea').focus(function () {
            if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
                $(this).val('').removeClass('hasPlaceholder');
            }
        }).blur(function () {
            if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
                $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
            }
        });
        $('input[type="text"], textarea').blur();
        $(active).focus();
        $('form').submit(function () {
            $(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
        });
    }
});

HTML:

代码语言:javascript
代码运行次数:0
运行
复制
                <li>
                    <input type="text" placeholder="To:" id="to" autocapitalize="off" autocorrect="off" autocomplete="off" />
                </li>
                <li>
                    <textarea placeholder="Message:" id="body" rows="10" cols="30" autocapitalize="off" autocorrect="off" autocomplete="off"></textarea>
                </li>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-08-01 04:36:21

$(':text')更改为$('input[type="text"], textarea')

或者,这个现有的jQuery插件:

https://github.com/mathiasbynens/jquery-placeholder

适用于文本和多种类型的输入字段,包括密码字段

票数 6
EN

Stack Overflow用户

发布于 2013-06-10 20:16:31

代码语言:javascript
代码运行次数:0
运行
复制
<script type="text/javascript">
 $(function() {
    if(!$.support.placeholder) { 
        var active = document.activeElement;
        $('input[type="text"], textarea').focus(function () {
            if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
                $(this).val('').removeClass('hasPlaceholder');
            }
        }).blur(function () {
            if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
                $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
            }
        });
        $('input[type="text"], textarea').blur();
        $(active).focus();
        $('form').submit(function () {
            $(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
        });
    }
});
</script>
票数 2
EN

Stack Overflow用户

发布于 2012-08-01 04:34:07

它正在使用text selector,请将其更改为也使用textarea。

代码语言:javascript
代码运行次数:0
运行
复制
$(':text')

应该是

代码语言:javascript
代码运行次数:0
运行
复制
$(':text, textarea')

或供better performance使用

代码语言:javascript
代码运行次数:0
运行
复制
$('input[type=text], textarea')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11748531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档