簡體   English   中英

如何創建CSS類以使輸入字段為只讀

[英]How to create a CSS class to make input field is readonly

我想創建一個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>

您無需使用CSS做任何事情。 html輸入中已經存在一個只讀屬性:

<input type="text" name="country" value="Norway" readonly>

在這里查看更多

沒有這樣的CSS屬性可以簡單地將輸入設為只讀,但是在此問題中 ,@ James Donnelly提供了一種解決方法來實現:

.enableInput{
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;          
}

是的,您可以使用:disabled偽類,例如

input.myClass:disabled {
/* style declaration here */

}

或者您可以使用此處建議的解決方案

你只能在jQuery中做到

function changeCss()
{ 
$('#desg').prop('disabled', true);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM