本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
在javascript中,想要设置input框为只读,只需要使用setAttribute()方法给input元素添加只读属性--readOnly即可。
setAttribute() 方法添加指定的属性,并为其赋指定的值。
语法:
复制代码1
element.setAttribute(attributename,attributevalue)
登录后复制
示例:
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!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学习教程】
以上就是javascript怎么设置input框为只读的详细内容,更多请关注靠谱客其它相关文章!
最后
以上就是害怕黄蜂最近收集整理的关于javascript怎么设置input框为只读的全部内容,更多相关javascript怎么设置input框为只读内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复