简体   繁体   English

在输入焦点的特定 div 上添加另一个类

[英]Add another class on a specific div on input focus

I'm trying to find a solution for my searchbar problem.我正在尝试为我的搜索栏问题寻找解决方案。 The searchbar should behave differently on smartphones so I need to add another class to the div, where the searchbar is placed.搜索栏在智能手机上的行为应该有所不同,因此我需要向放置搜索栏的 div 添加另一个类。

When I click on the input field, the div with the class "highlighted" should get another class.当我点击输入字段时,类“突出显示”的 div 应该得到另一个类。 Until now I just found solution to add a class to a next or partent div, but that's not what I was searching for.直到现在,我才找到了将类添加到下一个或partent div 的解决方案,但这不是我正在寻找的。

 <div class="highlighted"> <div class="region region-highlighted"> <section id="block-x" class="contextual-region block clearfix"> <div data-contextual-id="block:block=x:langcode=en|block_content:block_content=20:changed=1462269631&amp;ds_bundle=basic&amp;ds_view_mode=default&amp;langcode=en"></div> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"> <p>x</p> </div> </section> <div class="search-block-form contextual-region block block-search" data-drupal-selector="search-block-form" id="block-bootstrap-search" role="search"> <div data-contextual-id="block:block=bootstrap_search:langcode=en"></div> <form action="/search/node" method="get" id="search-block-form" accept-charset="UTF-8"> <div class="form-item js-form-item form-type-search js-form-type-search form-item-keys js-form-item-keys form-no-label form-group"> <label for="edit-keys--2" class="control-label sr-only">Search</label> <div class="input-group"> <input title="Enter the terms you wish to search for." data-drupal-selector="edit-keys" class="form-search form-control" placeholder="Search" type="search" id="edit-keys--2" name="keys" value="" size="15" maxlength="128"><span class="input-group-btn"><button type="submit" value="Search" class="button js-form-submit form-submit btn-primary btn icon-only" name=""><span class="sr-only">Search</span><span class="icon glyphicon glyphicon-search" aria-hidden="true"></span> </button> </span> </div> <div id="edit-keys--2--description" class="description help-block">Enter the terms you wish to search for.</div> </div> <div class="form-actions form-group js-form-wrapper form-wrapper" data-drupal-selector="edit-actions" id="edit-actions"></div> </form> </div> </div> </div>

Not sure what you're trying, but something like $('.highlighted').addClass('otherclass');不确定您在尝试什么,但类似于$('.highlighted').addClass('otherclass'); should work.应该管用。

Edit after comment:评论后编辑:

$('.input-group input').focus(function () { $('.highlighted').addClass('open'); })
$('.input-group input').blur(function () {$('.highlighted').removeClass('open'); });

(The only change needed to your code was the last selector. I'm just putting it in 2 lines for clarity.) (您的代码唯一需要更改的是最后一个选择器。为了清楚起见,我只是将它放在 2 行中。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果具有特定类的div具有iframe,则将另一个类添加到div - if div with specific class has an iframe add another class to the div jQuery 如果另一个 div 具有特定的 class,如何将 class 添加到一个 div - jQuery How to add a class to a div if another div has specific class 我想在输入后添加div if(hover或focus)然后如果我点击新div让div显示出来点击另一个元素或焦点 - i want to add div after input if (hover or focus) then if i clicked on the new div let the div displayed up to click on another element or focus 如果另一个 div 具有特定文本,则将类添加到 div - Add class to a div if another div has specific text 如果同一类有多个输入,如何使用jquery“ append”在“ focus”输入之后添加div? - How to use jquery 'append' to add div after an 'focus'ed input, if there are more than one input with same class? 将另一个类添加到div - Add another class to a div 如何使用jQuery将焦点添加到已从中删除类的div中包含的输入 - How to add focus to the input contained in the div from which a class have been removed using jQuery 输入焦点字段,然后在页面加载时添加类 - Input focus field then add class on page load 如果另一个 div 有 class,则将 class 添加到一个 div - Add class to a div if another div has a class 如果input或div被禁用,则添加一个类 - If input or div is disabled then add a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM