zoukankan      html  css  js  c++  java
  • html5 textarea 的 placeholder 换行的几种方式

    html5 textarea 的 placeholder 换行的几种方式

    在最近的项目中,设计图中的一个textarea文本输入框中要求提示文本多行显示,百度一下,总结出几个textarea文本输入框提示文本换行的方法

    1、在placeholder中的文字换行输入
    
    <textarea  placeholder="这是
                            一段
                            测试
                            文本"></textarea>
    
    
     
    2、使用 &#10;
    <textarea rows="4" placeholder="这是&#10;一段&#10;测试&#10;文本"></textarea>
    
     
    3、使用js控制

    html

    
    <textarea id="textarea"></textarea>
    
    

    js

    
    var placeholder = '这是一段
    测试文本';
    $('#textarea').attr('value', placeholder);
    
    $('#textarea').focus(function(){
        if($(this).val() === placeholder){
            $(this).attr('value', '');
        }
    });
    
    $('#textarea').blur(function(){
        if($(this).val() ===''){
            $(this).attr('value', placeholder);
        }    
    });
    
    
    http://jsfiddle.net/airandfingers/pdXRx/247/
    4、使用div模拟提示

    css

    #textAreaWrap {
        position: relative;
        background-color: white;
    }
    
    #textArea {
        position: relative;
        z-index: 1;
         350px;
        height: 100px;
        min-height: 100px;
        padding: 6px 12px;
        resize: vertical;
        background-color: transparent;
        border: 1px solid #a5a5a5;
    }
    
    #placeholderDiv {
        position: absolute;
        top: 0;
        padding: 6px 13px;
        color: #a5a5a5;
    }
    

    html

    <div id="textAreaWrap">
        <textarea id="textArea"></textarea>
        <div id="placeholderDiv">这是一段<br>
                             测试文本<br>
        </div>
    </div>
    

    js

    $(document).on('input', '#textArea', function () {
        if ($('#textArea').val()) {
            $('#placeholderDiv').hide();
        } else {
            $('#placeholderDiv').show();
        }   
    });
    
    这是一段
    测试文本
  • 相关阅读:
    MySQL表之间的关系概述
    网路通信简介
    多态与鸭子类型
    组合与类继承
    类与对象的属性与方法以及封装
    对象与类的初始
    2018.12.12
    2018.12.9浮动布局,盒子显隐,定位,z-index,流式布局,小米开头
    2018.12.8浮动布局,display总结,overflow,清浮动
    2018.12.7边界圆角redius,背景图设置,平铺,精灵图,盒子伪类索引
  • 原文地址:https://www.cnblogs.com/luozhangshuai/p/7590718.html
Copyright © 2011-2022 走看看