javascript如何设置input框为写保护

lxf2023-03-17 17:21:01

设定方式:1、应用“document.getElementById(id)”句子依据特定id值获得input原素目标;2、应用“input目标.setAttribute("readOnly", true)”句子给input原素加上写保护款式。

javascript如何设置input框为写保护

实例教程作业环境:windows7系统软件、javascript1.8.5版、Dell G3计算机。

在javascript中,需要设定input框为写保护,只需要使用setAttribute()方式给input原素加上只读属性--readOnly就可以。

setAttribute() 方式加上特定的特性,并为其赋特定数值。

词法:

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>

javascript如何设置input框为写保护

【相关信息:javascript学习教程

以上就是关于javascript如何设置input框为写保护的具体内容,大量欢迎关注AdminJS其他类似文章!