我有一個這樣的輸入:HTML - 保持佔位符,當用戶鍵入
<input value="My text" placeholder="Placeholder">
當我輸入類型的東西佔位符文本會消失,這是相當明顯的。現在
,我想要做的就是我想要的佔位符文本留在用戶的類型,所以你可以看到佔位符文本與原始文本後面的背景文字:
編輯:我也希望能夠使用JavaScript更改背景文本。
我有一個這樣的輸入:HTML - 保持佔位符,當用戶鍵入
<input value="My text" placeholder="Placeholder">
當我輸入類型的東西佔位符文本會消失,這是相當明顯的。現在
,我想要做的就是我想要的佔位符文本留在用戶的類型,所以你可以看到佔位符文本與原始文本後面的背景文字:
編輯:我也希望能夠使用JavaScript更改背景文本。
很難想象這種行爲的好用例,因爲它阻止了一些用戶的輸入。
一個簡單的方法是使用input::after
,但現在任何瀏覽器都不支持此功能(謝謝@ JukkaK.Korpela)。
但是你可以使用一個包裝元素和數據屬性,如下所示:
<div class="placeholder" data-placeholder="my placeholder">
<input value="My text" />
</div>
有了這個CSS:
.placeholder
{
position: relative;
}
.placeholder::after
{
position: absolute;
left: 5px;
top: 3px;
content: attr(data-placeholder);
pointer-events: none;
opacity: 0.6;
}
,導致:
既然你將不得不做調整,使這個看起來好了很多,你也可以考慮使用包裹<div>
元素作爲輸入「看起來很像」:
<div class="editable" data-placeholder="my placeholder">
<input type="text" value="my Text" />
</div>
CSS:
.editable
{
position: relative;
border: 1px solid gray;
padding: 3px;
background-color: white;
box-shadow: rgba(0,0,0,0.4) 2px 2px 2px inset;
}
.editable > input
{
position: relative;
z-index: 1;
border: none;
background-color: transparent;
box-shadow: none;
width: 100%;
}
.editable::after
{
position: absolute;
left: 4px;
top: 5px;
content: attr(data-placeholder);
pointer-events: none;
opacity: 0.5;
z-index: 1;
}
更好的解決方案,通過CSS輕鬆實現效果。請看:http://jsfiddle.net/csdtesting/wbqq129q/
郵編:
#login {
font-size: 12px;
margin: 0 auto;
width: 700px;
}
#login li {
float: left;
list-style: none;
margin-left: 30px;
position: relative;
}
#login li:first-child {
margin-left: 0;
}
label {
line-height: 40px;
position: absolute;
right: 120px;
top: 0;
bottom: 0;
-moz-transition: 0.3s right ease;
-ms-transition: 0.3s right ease;
-o-transition: 0.3s right ease;
-webkit-transition: 0.3s right ease;
transition: 0.3s right ease;
z-index: 0
}
input {
color: transparent;
font-size: 12px;
height: 35px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
-moz-transition: 0.3s all ease;
-ms-transition: 0.3s all ease;
-o-transition: 0.3s all ease;
-webkit-transition: 0.3s all ease;
transition: 0.3s all ease;
}
input[type="email"],
input[type="password"] {
border: 1px solid #ccc;
height: 35px;
padding: 0 10px;
width: 240px;
position: relative;
-moz-box-shadow: inset 0 0 10px rgba(0, 0, 0, .06);
-webkit-box-shadow: inset 0 0 10px rgba(0, 0, 0, .06);
box-shadow: inset 0 0 10px rgba(0, 0, 0, .06);
z-index: 2;
}
input[type="email"] {
color: rgba(47, 130, 194, .8);
}
/* Placeholder */
input[type="email"]:-moz-placeholder {
color: rgba(47, 130, 194, .6);
}
input[type="email"]:-ms-input-placeholder {
color: rgba(47, 130, 194, .6);
}
input[type="email"]::-webkit-input-placeholder {
color: rgba(47, 130, 194, .6);
}
/* Label */
input[type="email"] + label {
color: rgb(47, 130, 194);
}
input:focus + label {
right: 10px;
}
input[type="email"]:focus,
input[type="password"]:focus {
background-color: rgba(255, 255, 255, .8);
}
/* Submit */
input[type="submit"] {
background-color: #333;
background: -moz-linear-gradient(bottom, #333, #444);
background: -ms-linear-gradient(bottom, #333, #444);
background: -o-linear-gradient(bottom, #333, #444);
background: -webkit-linear-gradient(bottom, #333, #444);
background: linear-gradient(bottom, #333, #444);
border: 1px solid #222;
color: #fff;
cursor: pointer;
height: 35px;
width: 110px;
}
<form id="login">
<ul>
<li>
<input id="email" name="email" placeholder="Your Email" title="Your Email" type="email" required />
<label for="email">Your Email</label>
</li>
</ul>
</form>
但這不是我想要達到的。 – super 2014-10-12 11:11:08
這可以通過使用 '的onchange' 處理程序來完成。你將編寫一個奇特的函數,將佔位符的其餘部分連接到用戶輸入的內容上,並將光標放在用戶文本的末尾。
下面是一些未經檢驗的,不完整的JS /僞代碼給你一個想法:
userTextLength: 0, // measure of how many chars the user has typed; need this because the length itself won't be a valid measure, since we're modifying it in place. Note that we're using the DOM as a source of truth here... alternative method would be to store the user's text itself here, but let's run with this.
placeholder: "xx/yy/zz",
onchange: function() {
boxText = document.querySelector('#elem').value;
if (boxText.length === 1) { // special handling for the first character they type. (Using placeholder text at first.)
this.userTextLength++;
placeholder = boxText.slice(userTextLength);
userText = boxText.slice(0, userTextLength);
document.querySelector('#elem').innerHTML = userText + placeholder;
}
if (boxText.length < placeholder.length) { // this would mean they used backspace, which also needs to be handled.
}
else { // the normal case, should look quite similar to the first if block
this.userTextLength += 1;
userInput =
}
}
東西我還沒有在這裏處理是光標聚焦。這將需要一個'onfocus'事件,並且還將使用userTextLength屬性來決定將它放在哪裏。對於這樣做的一些幫助,this answer看起來應該是有幫助的。
我認爲這不是一個好的做法。你可以使用,它將保持在那裏。或者在用戶開始輸入時只顯示輸入標題
@Meer你能舉個例子嗎?像小提琴一樣? – super 2014-10-12 10:46:47
@Tambo你能舉個例子嗎?像小提琴一樣? – super 2014-10-12 10:47:17