2014-10-20 85 views
0

例如,我有一个文本输入以“在这里插入文本”像这样一个值:自动删除输入值

<input type="text" value="insert text here" /> 

当我点击输入的块,然后的“文本“在这里插入文字”会自动消失,但是当用户取消在那里写东西时,文字会再次出现。

如何做到这一点?

感谢

+4

您可以使用HTML占位符。 [更多](http://www.w3schools.com/tags/att_input_placeholder.asp) – Ruddy 2014-10-20 07:37:30

+2

占位符很棒!谢谢!解决了! – 2014-10-20 07:41:16

+0

如果以下答案对您有帮助,您应该标出解决方案。标记任何适合你的人 – BNN 2014-10-20 07:45:14

回答

1

使用占位符属性
<input name="" type="text" placeholder="place holder text here."/>

但它可能不适用于较旧的浏览器。如果您使用的是较旧的浏览器,则应该使用javascrip/jquery来处理此问题。

<input type="text" class="userName" value="Type User Name..." /> 


$(document).ready(function(){ 

    $('.userName').on('focus',function(){ 
    var placeHolder = $(this).val(); 
     if(placeHolder == "Type User Name..."){ 
      $(this).val(""); 
     } 
    }); 
    $('.userName').on('blur',function(){ 
    var placeHolder = $(this).val(); 
     if(placeHolder == ""){ 
      $(this).val("Type User Name..."); 
     } 
    }); 

}); 

看到德莫JSFIDDLE

1

使用该placeholder属性

<input type="text" placeholder="insert text here" /> 
0
<input type="text" name="fname" placeholder="insert text here"> 
0

使用占位符而不是值。

<input type="text" placeholder="insert text here" /> 
0

你可以做到这一点使用Placehoder 但你需要知道旧版本的浏览器,因为它不支持的地方HODER

<input type="text" value="some text" placeholder="insert text here"/> 

对旧版本的浏览器u可以使用的下面的链接并下载JS

link

-1
If you want compatible in all browsers then you can use this code. 
<!DOCTYPE html> 
<html> 
<body> 

First Name: <input type="text" id="myText" placeholder="Name"> 

<p>Click the button to display the placeholder text of the text field.</p> 

<button onclick="myFunction()">Try it</button> 

<p id="demo"></p> 

<script> 
function myFunction() { 
    var x = document.getElementById("myText").placeholder; 
    document.getElementById("demo").innerHTML = x; 
} 
</script> 

</body> 
</html> 
+0

它只是w3schools的复制粘贴,你应该解释它。它也可以在没有javascript的情况下实现 – BNN 2014-10-20 07:47:14

+0

是的,我知道它只是复制粘贴,但它是问题的解决方案。 – Websolutionexpert24 2014-10-20 07:48:29

+0

@Nadeem你也给解决方案作为复制粘贴,你也可以检查你的代码 – Websolutionexpert24 2014-10-20 07:50:33