javascript怎么设置input框为只读
生活随笔
收集整理的這篇文章主要介紹了
javascript怎么设置input框为只读
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
設置方法:1、使用“document.getElementById(id)”語句根據指定id值獲取input元素對象;2、使用“input對象.setAttribute("readOnly", true)”語句給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學習教程】
總結
以上是生活随笔為你收集整理的javascript怎么设置input框为只读的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android的Recovery中fon
- 下一篇: Css 特殊或不常用属性