如何创建一个CSS类来生成输入字段是readonly

cha*_*aya 0 javascript css

我想创建一个CSS类,使输入字段是只读的.

这是我的尝试:

 <style>
 .enableInput input[readonly] {
  color: #CB000F;
 }
</style>
<script>
function changeCss()
{
$("#desg").addClass("enableInput input");
}
</script>
</head>
FirstName:<input type="text" name='fname' id='fname' onBlur="changeCss();">
Designation:<input type="text" name='desg' id='desg' value='Software Engr'>
</html>
Run Code Online (Sandbox Code Playgroud)

Abd*_*bar 5

你不需要用CSS做任何事情.html输入中已存在readonly属性:

<input type="text" name="country" value="Norway" readonly>
Run Code Online (Sandbox Code Playgroud)

在这里查看更多

没有这样的CSS属性只能简单地输入,但在这个问题中,@ James Donnelly给出了一个解决方法来实现:

.enableInput{
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;          
}
Run Code Online (Sandbox Code Playgroud)