javascript - 通过 css 和::focus 进行嵌套选择

标签 javascript css focus

Bellow,当 selectize-input 类的 div 元素处于焦点上时,有什么方法可以使用 css 选择 helpTextInvalid 类吗?

 <html>
  <head>
    <body>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js">               
            </script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.js">               
            </script>
      <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.css" />
      <form id="select1-selectBloco" autocomplete="off" class="form-group shiny-input-container" style="padding-top:5px; width:100%; height:64px;">
        <label style="width:100%" class="labelCustom">
          <div class="selectize-control demo inputSelect single">
              <div class="selectize-input items not-full has-options" style="min-height: 34px;">
              <input type="select-one" autocomplete="off" tabindex="" id="select1-selectInput-selectized" style="width: 4px;">
              </div>
         </div>
          <span id="select1-selectValidHelp" class="helpTextValid">Select an option 1.</span>  
          <span id="select2-selectValidHelp" class="helpTextInvalid">Select an option 2.</span> 
      </label>
    </form>
    <script>
            $('#select1-selectInput').selectize({});
    </script>
  </body>
</head>
</html>

提前致谢!

最佳答案

您可以使用 jQuery 的 .focusin() 函数: https://api.jquery.com/focusin/

$(".selectize-input").focusin(function() {
  $('#select1-selectInput').selectize({});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
  <head>
    <body>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js">               
            </script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.js">               
            </script>
      <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.css" />
      <form id="select1-selectBloco" autocomplete="off" class="form-group shiny-input-container" style="padding-top:5px; width:100%; height:64px;">
        <label style="width:100%" class="labelCustom">
          <div class="selectize-control demo inputSelect single">
              <div class="selectize-input items not-full has-options" style="min-height: 34px;">
              <input type="select-one" autocomplete="off" tabindex="" id="select1-selectInput-selectized" style="width: 4px;">
              </div>
         </div>
          <span id="select1-selectValidHelp" class="helpTextValid">Select an option 1.</span>  
          <span id="select2-selectValidHelp" class="helpTextInvalid">Select an option 2.</span> 
      </label>
    </form>
  </body>
</head>
</html>

关于javascript - 通过 css 和::focus 进行嵌套选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59307360/

相关文章:

javascript - HTML5 Canvas + JavaScript 线框球体转换问题

javascript - {...object, property : value} work with spread syntax? 如何

javascript - Next.js - `npm run build` 失败

css - 固定标题 DIV 在没有设置边距的情况下向下推

html - 使用外部链接的 CSS 将边框应用于 html 元素

c# - Compact Framework 3.5 文本字段设置焦点不起作用

android - 键盘出现时 ListActivity 中的编辑文本 ListView 失去焦点

javascript - 多个 <option> 相互连接

jquery - 如何处理特定于设备的花式框或对话框宽度?

html - 专注于粘性条强制页面中的输入以滚动到顶部