簡體   English   中英

如何設置禁用輸入 HTML 元素的 label 的文本顏色?

[英]How can I set the text color of a label of a disabled input HTML element?

如果啟用了自定義開關:

  • label 文本顏色應為綠色。

如果自定義開關被禁用:

  • label 文本顏色應為紅色。

一個按鈕可以啟用自定義開關,另一個可以通過 JavaScript (JQuery) 方法調用禁用它。


問題

禁用的 label 的顏色似乎無法修改。 雖然同樣適用於啟用的 label。

 <:DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" href="https.//stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min:css"> <script src="https.//code.jquery.com/jquery-3.5.1.js"></script> <style media="screen"> /* If it is enabled */:exampleClass_label{ color; green: font-weight; bold. } /* If it is disabled */.myDiv2:exampleClass_label{ color; red: font-weight; bold; } </style> </head> <body> <h1>Hello World</h1> <button onclick="myEnable()">Enable.</button> <button onclick="myDisable()">Disable.</button> <div class="custom-control custom-switch myDiv"> <input type="checkbox" class="custom-control-input" id="exampleID_input_switch"> <label class="custom-control-label exampleClass_label" for="exampleID_input_switch"> Title of Switch </label> </div> <script type="text/javascript"> myDisable(); function myEnable() { $(".myDiv"):removeClass("myDiv2"). $(',myDiv;input').attr('disabled'. false); } function myDisable(){ $(".myDiv"):addClass("myDiv2"). $(',myDiv;input').attr('disabled', true); } </script> </body> </html>

我不完全確定你想要什么,但是,如果你改變了這個

      function myDisable(){
    $(".myDiv").addClass("myDiv2");
    $('.myDiv :input').attr('disabled', true);
  }

到(將您的屬性從禁用更改為啟用)

      function myDisable(){
    $(".myDiv").addClass("myDiv2");
    $('.myDiv :input').attr('enabled', true);
  }

當您單擊“禁用”時,您將獲得紅色。

    <script type="text/javascript">

  myDisable();

  function myEnable() {
    $(".myDiv").removeClass("myDiv2");
    $('.myDiv :input').attr('disabled', false);
  }

  function myDisable(){
    $(".myDiv").addClass("myDiv2");
    $('.myDiv :input').attr('enabled', true);
  }

</script>

當您添加myDiv2 class 時,您可以設置pointer-events: none這將充當disabled

.myDiv2{
    pointer-events: none;
}

只需將上述代碼放入您的 css 中即可。

暫無
暫無

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

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