如何在开始输入后删除 HTML 输入框的默认文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12417040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:49:55  来源:igfitidea点击:

How do I make a HTML input box default text remove AFTER starting to type

htmlinputinputbox

提问by Jordan Clifton

Say I have an input box with default text "name" in it. I would like for the user to click the box, the text stays in the box until the user starts to type.

假设我有一个带有默认文本“名称”的输入框。我希望用户单击该框,文本保留在框中,直到用户开始键入。

I am notlooking for code to clear the input box after focusing it.

不是在寻找在聚焦后清除输入框的代码。

Thanks!

谢谢!

回答by alexndm

It's placeholder attribute

它是占位符属性

<input type="text" placeholder="Type your name">

回答by Steven

What you are trying to do depends on the version of the browser you are targeting. If the customer uses a browser that support html5 then the answer from @alexndm will work. However if you are targeting a mass audience with number of browser version.

您尝试执行的操作取决于您所针对的浏览器版本。如果客户使用支持 html5 的浏览器,那么来自 @alexndm 的答案将起作用。但是,如果您针对具有多个浏览器版本的大量受众。

This article here will show how to do it the old way, i.e. using javascript. How do I get placeholder text in firefox and other browsers that don't support the html5 tag option?

此处的这篇文章将展示如何以旧方式进行操作,即使用 javascript。如何在不支持 html5 标签选项的 Firefox 和其他浏览器中获取占位符文本?

Note: the OP of the post I linked also have html5 in case you need it.

注意:我链接的帖子的 OP 也有 html5,以备不时之需。