代码之家  ›  专栏  ›  技术社区  ›  HenryM

使用特定类获取下一个<div>并移除一个类

  •  0
  • HenryM  · 技术社区  · 6 年前

    我正试图编写一些jQuery,以便在 <div> 如果选择了特定的类(表单域),则 < ,与另一个类(窗体字段dropdwon)一起删除了另一个css类(hide)。

    这是html

                 <div class="col-sm">
                    <h2 class="section-title">
                      Header                    </h2>
                    <input type="hidden" name="" value=""  />
                    <div class="form-field">
                      Header <i class="fa fa-caret-down orange"></i>
                      <div class="form-field-dropdown hide">
                        <div class="select-option selected">
                          Option
                        </div>
                        <div class="select-option">
                          Option
                        </div>
                        <div class="select-option">
                          Option
                        </div>
                        <div class="select-option">
                          Option
                        </div>
                        <div class="select-option">
                          Option
                        </div>
                      </div>
                    </div>
    

    这是我正在使用的jQuery:

    <script>
        $(".form-field").click(function() {
            $(this).next().find(".form-field-dropdown").removeclass('hide');
        });
    </script>
    

    <分区>

    4 回复  |  直到 6 年前
        1
  •  3
  •   charan kumar    6 年前

    表单字段下拉菜单div不是下一个同级,因此next()不起作用,

    使用find函数来实现这一点,以便它可以在父级中找到“.form field”下拉列表。form field

    <script>
        $(".form-field").click(function() {
            $(this).find(".form-field-dropdown").removeClass('hide');
        });
    </script>
    

        2
  •  2
  •   cloned    6 年前

    示例代码中没有next()元素。根据代码的结构,您可能希望找到 form-field-dropdown 直接在里面 form-field 上课。

    所以正确的JS应该是: $(this).find(".form-field-dropdown").removeClass('hide')

    这能解决你的问题吗?

        3
  •  1
  •   Programmer    6 年前

    next() this ). 只需移除 它应该能起作用。

    <script>
        $(".form-field").click(function() {
            $(this).find(".form-field-dropdown").removeclass('hide');
        });
    </script>
    
        4
  •  0
  •   Alexander N. Morgunov    6 年前

    .next()

    $(".form-field").click(function() {
        $(this).find(".form-field-dropdown").removeClass('hide');
    });
    

    如果仍然不能按预期工作,那么请检查CSS以发现一些错误。然后签入元素检查器,html中发生了什么。