如何用JavaScript编写代码来控制表单文字的显示与隐藏?

更新于
2026-09-27 22:25:02
1阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计850个文字,预计阅读时间需要4分钟。

如何用JavaScript编写代码来控制表单文字的显示与隐藏?

本文以家庭分享为例,介绍了JavaScript实现显示隐藏表格的具体代码。供大家参考,内容如下:

实现思路:

1.使用onfocus和onblur事件来控制表格的显示和隐藏。

2.使用onfocus事件来获取焦点,触发显示表格。

3.使用onblur事件来失去焦点,触发隐藏表格。

代码示例:

显示隐藏表格示例 #myTable { display: none; width: 100%; border-collapse: collapse; } #myTable th, #myTable td { border: 1px solid #ddd; padding: 8px; }

姓名 年龄 性别 张三 25 男 李四 30 女

使用说明:

1.将以上代码保存为HTML文件。

2.打开HTML文件,在输入框中输入文字,点击输入框时表格会显示,失去焦点时表格会隐藏。

本文实例为大家分享了JavaScript实现显示隐藏表单文字的具体代码,供大家参考,具体内容如下

实现思路

运用 onfocus、onblur 事件

onfocus- - -获取焦点(鼠标点击输入框,输入框里面有闪动的光标)

onblur- - -失去焦点(鼠标不选中输入框,输入框里面失去闪动的光标)

1、给输入框设置一个默认值

如何用JavaScript编写代码来控制表单文字的显示与隐藏?

2、获取输入框对象,给其绑定事件:onfocus 和 onblur
当获取焦点时(onfocus)- - -判断输入框的value值是否是默认值,如果是默认值初始值,将value改为空,默认初始值就没有了,可以输入自己的
当时去焦点时(onblur)- - -判断输入框的value值是否没有值,没有的话,给value赋值默认值,未输入内容移开后又会显示默认值了

3、还可以给获取焦点和失去焦点两种状态的文字颜色设置不同,让两种状态区分的更明显

示例1:

代码:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>获取/失去焦点</title> <style> input { color: #ccc; outline: none; } </style> </head> <body> <input type="text" value="手机"> <script> var text = document.querySelector('input'); text.onfocus = function() { if (this.value === '手机') { this.value = ''; } this.style.color = '#333'; } text.onblur = function() { if (this.value === '') { this.value = '手机'; } this.style.color = '#ccc'; } </script> </body> </html>

页面效果:

示例2

代码:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>用户名 显示 隐藏</title> <style> input { font-size: 14px; color: #999; outline: none; padding: 3px 0 3px 10px; border: 1px solid #ccc; } </style> </head> <body> <!--用户名 显示隐藏内容 --> <input type="text" value="邮箱/ID/手机号" class="userName"> <script> var userName = document.querySelector('.userName'); userName.onfocus = function() { if (this.value === '邮箱/ID/手机号') { this.value = ''; this.style.borderColor = 'pink'; } else { this.style.borderColor = 'pink'; this.style.color = '#999'; } } userName.onblur = function() { if (this.value === '') { this.value = '邮箱/ID/手机号'; this.style.borderColor = '#ccc'; this.style.color = '#999'; } else { this.style.borderColor = '#ccc'; this.style.color = '#333'; } } </script> </body> </html>

页面效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计850个文字,预计阅读时间需要4分钟。

如何用JavaScript编写代码来控制表单文字的显示与隐藏?

本文以家庭分享为例,介绍了JavaScript实现显示隐藏表格的具体代码。供大家参考,内容如下:

实现思路:

1.使用onfocus和onblur事件来控制表格的显示和隐藏。

2.使用onfocus事件来获取焦点,触发显示表格。

3.使用onblur事件来失去焦点,触发隐藏表格。

代码示例:

显示隐藏表格示例 #myTable { display: none; width: 100%; border-collapse: collapse; } #myTable th, #myTable td { border: 1px solid #ddd; padding: 8px; }

姓名 年龄 性别 张三 25 男 李四 30 女

使用说明:

1.将以上代码保存为HTML文件。

2.打开HTML文件,在输入框中输入文字,点击输入框时表格会显示,失去焦点时表格会隐藏。

本文实例为大家分享了JavaScript实现显示隐藏表单文字的具体代码,供大家参考,具体内容如下

实现思路

运用 onfocus、onblur 事件

onfocus- - -获取焦点(鼠标点击输入框,输入框里面有闪动的光标)

onblur- - -失去焦点(鼠标不选中输入框,输入框里面失去闪动的光标)

1、给输入框设置一个默认值

如何用JavaScript编写代码来控制表单文字的显示与隐藏?

2、获取输入框对象,给其绑定事件:onfocus 和 onblur
当获取焦点时(onfocus)- - -判断输入框的value值是否是默认值,如果是默认值初始值,将value改为空,默认初始值就没有了,可以输入自己的
当时去焦点时(onblur)- - -判断输入框的value值是否没有值,没有的话,给value赋值默认值,未输入内容移开后又会显示默认值了

3、还可以给获取焦点和失去焦点两种状态的文字颜色设置不同,让两种状态区分的更明显

示例1:

代码:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>获取/失去焦点</title> <style> input { color: #ccc; outline: none; } </style> </head> <body> <input type="text" value="手机"> <script> var text = document.querySelector('input'); text.onfocus = function() { if (this.value === '手机') { this.value = ''; } this.style.color = '#333'; } text.onblur = function() { if (this.value === '') { this.value = '手机'; } this.style.color = '#ccc'; } </script> </body> </html>

页面效果:

示例2

代码:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>用户名 显示 隐藏</title> <style> input { font-size: 14px; color: #999; outline: none; padding: 3px 0 3px 10px; border: 1px solid #ccc; } </style> </head> <body> <!--用户名 显示隐藏内容 --> <input type="text" value="邮箱/ID/手机号" class="userName"> <script> var userName = document.querySelector('.userName'); userName.onfocus = function() { if (this.value === '邮箱/ID/手机号') { this.value = ''; this.style.borderColor = 'pink'; } else { this.style.borderColor = 'pink'; this.style.color = '#999'; } } userName.onblur = function() { if (this.value === '') { this.value = '邮箱/ID/手机号'; this.style.borderColor = '#ccc'; this.style.color = '#999'; } else { this.style.borderColor = '#ccc'; this.style.color = '#333'; } } </script> </body> </html>

页面效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。