javascript怎么设置input框为只读
导读:收集整理的这篇文章主要介绍了javascript怎么设置input框为只读,觉得挺不错的,现在分享给大家,也给大家做个参考。设置方法:1、使用“document.getElementById(id ”语句根据指定id值获取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框为只读的详细内容,更多请关注其它相关文章!
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: javascript怎么设置input框为只读
本文地址: https://pptw.com/jishu/593258.html