如何将JavaScript中的input框设置为只读状态?

2026-03-31 13:521阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

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

如何将JavaScript中的input框设置为只读状态?

设置方法:1、使用document.getElementById(id)语句根据指定id值获取input元素对象;2、使用input对象.setAttribute(readOnly, true)语句给input元素添加只读样式。+ 本教程操作环境:Windows 7系统

设置方法:1、使用“document.getElementById(id)”语句根据指定id值获取input元素对象;2、使用“input对象.setAttribute("readOnly", true)”语句给input元素添加只读样式。

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

在javascript中,想要设置input框为只读,只需要使用setAttribute()方法给input元素添加只读属性--readOnly即可。

setAttribute() 方法添加指定的属性,并为其赋指定的值。

语法:

如何将JavaScript中的input框设置为只读状态?

element.setAttribute(attributename,attributevalue)

示例:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <input type="text" id="text" /><br><br> <input type="button" value="设为只读" id="btn" /> <script> function my(id) { return document.getElementById(id); } my("btn").onclick = function() { my("text").setAttribute("readOnly", true); } </script> </body> </html>

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

如何将JavaScript中的input框设置为只读状态?

设置方法:1、使用document.getElementById(id)语句根据指定id值获取input元素对象;2、使用input对象.setAttribute(readOnly, true)语句给input元素添加只读样式。+ 本教程操作环境:Windows 7系统

设置方法:1、使用“document.getElementById(id)”语句根据指定id值获取input元素对象;2、使用“input对象.setAttribute("readOnly", true)”语句给input元素添加只读样式。

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

在javascript中,想要设置input框为只读,只需要使用setAttribute()方法给input元素添加只读属性--readOnly即可。

setAttribute() 方法添加指定的属性,并为其赋指定的值。

语法:

如何将JavaScript中的input框设置为只读状态?

element.setAttribute(attributename,attributevalue)

示例:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <input type="text" id="text" /><br><br> <input type="button" value="设为只读" id="btn" /> <script> function my(id) { return document.getElementById(id); } my("btn").onclick = function() { my("text").setAttribute("readOnly", true); } </script> </body> </html>