設(shè)置方法:1、使用“document.getElementById(id)”語句根據(jù)指定id值獲取input元素對(duì)象;2、使用“input對(duì)象.setAttribute("readOnly", true)”語句給input元素添加只讀樣式。
本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。
在javascript中,想要設(shè)置input框?yàn)橹蛔x,只需要使用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="設(shè)為只讀" id="btn" /> <script> function my(id) { return document.getElementById(id); } my("btn").onclick = function() { my("text").setAttribute("readOnly", true); } </script> </body> </html>
【