<textarea>输入框提示文字

背景

看了过时的资料,花费大把时间,不过也有收获,还是写写吧!

分析

有同学可能想到直接在<textarea>标签内输入帮助文字,但是这又有一个新问题——因为<textarea>设置了默认内容,如果用户不点击输入框(更改输入框的文本),而直接提交,就把默认的提示内容提交到后台了。

参考其他同学的解决方案:创建一个div,这个div包含了帮助文字、<textarea>,并通过css的position属性来控制,将帮助文字显示到<textarea>中,点击<textarea>(或提交)时,隐藏到帮助文字,这样即使用户不点击输入框直接提交,也不会把默认的提示文字提交到后台(因为帮助文字本身就与textarea不属于同一个层)。

示例

实现代码

<style>
        textarea {
            width: 200px;
            height: 200px;
        }

        div {
            width: 250px;
            display: inline-block;
        }

        .normal {
            border-right: solid 1px;
        }

        .normal small {
            color: #6da9a2;
        }

        .abnormal {
            position: absolute;
            margin-left: 40px;
        }

        #textArea {
            position: absolute;
        }

        .abnormal div {
            color: darkgrey;
            position: absolute;
        }
    </style>
<h2>textarea添加帮助文字</h2>
<hr/>
<div class="normal">
    <p><b>普通显示方式</b></p>
    <textarea></textarea><br/>
    <!--代码此处千万不要强迫症发作——按回车键!!!不然就会在文本框内自动保留空格-->

    <small>*在此输入备注信息</small>
</div>

<div class="abnormal">
    <p><b>类似placeholder的显示方式</b></p>
    <textarea id="textArea" onfocus="document.getElementById(‘introTxt‘).style.display=‘none‘"
              onblur="if(value==‘‘)document.getElementById(‘introTxt‘).style.display=‘inline‘"></textarea>
    <!--代码此处千万不要强迫症发作——按回车键!!!不然就会在文本框内自动保留空格-->
    <div id="introTxt" onclick="focusTextarea()">在此输入备注信息</div>

</div>

<script>
    /*这个函数是为了保证浏览者点击introTextarea这层div时,使焦点自动移动到textarea*/
    function focusTextarea() {
        document.getElementById(‘introTxt‘).style.display = ‘none‘;
        var temp = document.getElementById(‘textArea‘);
        temp.focus();
    }
</script>

页面效果


重点来了!!!

可以这么说,我上面写的你就当做没看见吧!

因为

看到没,我的亲!

<textarea>新增属性里面已经添加了placeholder属性,也就是说,我上面写了那么多,人家一句代码搞定!

实现代码

<textarea placeholder="这是帮助文字">
</textarea>

网页效果



知识要更新!

时间: 2024-08-05 17:55:58

<textarea>输入框提示文字的相关文章

输入框提示文字跨浏览器的placeholder-jQuery版

<script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script type="text/javascript" src="jquery.enplaceholder.js"></script> 例子展示: http://www.ifrans.cn/demo/enplaceholder.html jq

HTml输入框提示文字

1 <input type="password" placeholder="请输入密码" /> 效果如下

输入框提示文字js

<input style="margin-right: 0px; padding-right: 0px;" class="text" required="" value="请输入用户名" onclick="if(this.value=='请输入用户名'){this.value=''}" onblur="if(this.value==''){this.value='请输入用户名'}"

textarea 输入框限制字数

在textarea标签中,只需要设置maxlength=”***”即可,但是在textarea标签中,IE9及IE9以下浏览器是不支持的,IE10.IE11则支持,估计后续的版本应该都会支持. 现在来说下怎么让大部分IE版本都支持textarea 标签限制字数. 第一种方法: <textarea id="taContent" rows="3"  maxlength="20" onchange="this.value=this.va

input提示文字;placeholder字体修改

在很多网站上我们都看到input输入框显示提示文字,让我们一起来看看如果在input输入框中显示提示文字.我们只需要在<input>标签里添加:placeholder="提示文字即可". <style> /*修改提示文字的颜色*/ input::-webkit-input-placeholder { /* WebKit browsers */ color: red; } input:-moz-placeholder { /* Mozilla Firefox 4

winform Textbox模糊搜索实现下拉显示+提示文字

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _04TextBox { public partia

仿校内textarea输入框字数限制效果

这是一个仿校内textarea回复消息输入框限制字数的效果,具体表现如下: 普通状态是一个输入框,当光标获取焦点时,出现字数记录和回复按钮 PS:上边那个小三角可不是用的图片. 普通状态效果如下: 获取焦点时: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> &

js实例_当鼠标移动到某个元素上时在元素下面显示一段提示文字

效果: 当鼠标不在自动登录上时,只显示自动登录这个表单,下面的div提示则隐藏. 当鼠标移动到自动登录这个表单时,则显示下面的div提示,当鼠标移出时再将其隐藏 代码实现与原理: HTML代码: <body> <input type="checkbox" />自动登录 <div id="div1">不要再玩游戏啦!!!</div> </body> CSS代码:给id为div1的元素做一下修饰,并且将其最开始

ReportControl设置无显示内容时的提示文字

由于项目显示内容列表采用的是codejock software里面的ReportControl控件. 在没有显示内容时,提示文字"There are no items to show",想显示成中文的提示内容. 后台修改代码: this.reportControl控件名称.PaintManager.NoItemsText = "修改为自己的中文空行提示内容"; 即可! ReportControl设置无显示内容时的提示文字,布布扣,bubuko.com