占位符自动换行在输入字段中

时间:2016-09-29 03:42:07

标签: html css

我需要在输入字段中放置一个长placeholder文本。

但是,由于文字很长,placeholder会被删除。

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<label for="password">
  <input id="password" name="user[password]" placeholder="Password (at least 8 letters, numbers, or punctuation marks)" size="30" type="password">
</label>

有没有办法让它变成这个? enter image description here

3 个答案:

答案 0 :(得分:8)

&#13;
&#13;
id_orden | date | total | id_usuario | name | phone | maxedo
1        |15-may|50     | 1          | abc  | 999   | c
2        |20-may|60     | 2          | def  | 888   | b
&#13;
input{
  height:50px;
}

::-webkit-input-placeholder { /* Chrome/Opera/Safari */
  white-space:pre-line;  
  position:relative;
  top:-7px;
  
}
::-moz-placeholder { /* Firefox 19+ */
     white-space:pre-line;  
  position:relative;
  top:-7px;
}
:-ms-input-placeholder { /* IE 10+ */
    white-space:pre-line;  
  position:relative;
  top:-7px;
}
:-moz-placeholder { /* Firefox 18- */
     white-space:pre-line;  
  position:relative;
  top:-7px;
}
&#13;
&#13;
&#13;

答案 1 :(得分:3)

我已经尝试过分裂了。你也可以尝试一下:

var placeholder = '<div>Password </div> <div>(at least 8 letters, numbers, or<br> punctuation marks)</div>';
$("body").click(function(e){
	if($("#input").html() === ""){
    $("#input").html("<div>Password </div> <div>(at least 8 letters, numbers, or<br> punctuation marks)</div>");
    $("#input").css("color", "gray")
  }
});
$('#input').focus(function(){
    if($(this).html() === placeholder){
        $(this).html("");
        $(this).css("color", "black")
    }
});
#input {
    -moz-appearance: textfield;
    -webkit-appearance: textfield;
    background-color: white;
    background-color: -moz-field;
    border: 1px solid darkgray;
    box-shadow: 1px 1px 1px 0 lightgray inset;  
    font: -moz-field;
    font: -webkit-small-control;
    margin-top: 5px;
    padding: 2px 3px;
    width: 240px;
    height: 30px;
    color: gray;
}
#input div{  
    float: left;
    clear: none; 
    height: 100%;
    vertical-align:middle;  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="input" contenteditable><div>Password </div> <div>(at least 8 letters, numbers, or<br> punctuation marks)</div></div>

答案 2 :(得分:1)

我使用这样的占位符。你可以像任何你想要的那样包装它:

<label for="password">
  <input id="password" name="user[password]" placeholder="" type="password">
  <span class="placeholder">Password (at least 8 letters, numbers, or punctuation marks)</span>
</label>

并添加一点javascript和css:

jQuery(document).ready(function($){
    $('input').each(function(idx,el){
    $(el).width($(el).next('.placeholder').width());
    $(el).next('.placeholder').css("line-height",$(el).css("line-height"));
  });
  $('input').on("focus",function(e){
    $(this).next('.placeholder').css("visibility","hidden");
  });
  $('input').on("blur",function(e){
    if($(this).val().length == 0)
        $(this).next('.placeholder').css("visibility","visible");
  });
});

CSS:

label {
  position: relative;
}

.placeholder {
  position:absolute;
  left: 0;
  white-space: nowrap;
  padding: 2px 8px 0px;
  color: grey;
}
  

http://jsfiddle.net/pdXRx/906/

相关问题