我是靠谱客的博主 害怕黄蜂,这篇文章主要介绍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>
登录后复制

1.gif

【相关推荐:javascript学习教程

以上就是javascript怎么设置input框为只读的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是害怕黄蜂最近收集整理的关于javascript怎么设置input框为只读的全部内容,更多相关javascript怎么设置input框为只读内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(144)

评论列表共有 0 条评论

立即
投稿
返回
顶部