18910140161

js设置input为只读的方法

2021-12-11 13:06:05

132

html中的input输入框设置为只读的话,只需要添加 readonly 属性即可,但如果用js的触发事件来做该怎么实现呢,下面提供一种JS设置 input 为只读状态的方法。

原生js代码设置input为只读状态的方法

js示例代码:

<!DOCTYPE html>
<html>
<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>
</head>
<body>
<input type="text" id='mo' value="顺晟科技博客">
<button onclick="inputreadonly()">设置为只读取</button>
<script>
function inputreadonly(){
var mo = document.getElementById('mo');
mo.setAttribute("readOnly", true);
}
</script>
</body>
</html>

运行代码演示

js设置input为只读的方法

相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航