在输入焦点

时间:2016-05-04 14:52:37

标签: javascript jquery css twitter-bootstrap drupal

我正在尝试为我的搜索栏问题找到解决方案。搜索栏在智能手机上的行为应该不同,所以我需要在div中添加另一个类,其中放置了搜索栏。

当我点击输入字段时,带有“突出显示”类的div应该得到另一个类。到目前为止,我刚刚找到了将一个类添加到下一个或部分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>

1 个答案:

答案 0 :(得分:0)

不确定您在尝试什么,但$('.highlighted').addClass('otherclass');之类的内容应该有用。

评论后修改:

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

(您的代码所需的唯一更改是最后一个选择器。为了清晰起见,我只是将它放在2行中。)