内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>javascript - jQuery add class on hover on certain width if bootstrap dropdown is showing - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery add class on hover on certain width if bootstrap dropdown is showing - Stack Overflow

programmeradmin0浏览0评论

I am using bootstrap with dropdown. My anchor has a background color on hover. But when the dropdown is showing i want the parent containing the dropdown to lose the background color.

My HTML is:

  <nav class="navbar navbar-default av-nav" role="navigation">
     <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
            <span class="sr-only"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav nav nav-tabs">
                <li class="lia li1"><a href="#">Home</a></li>
                <li class="dropdown li2"><a class="dropdown-toggle" data-toggle="dropdown">About</a><span class="nav-arrow"></span>
                    <div class="dropdown-menu">
                        <ul>
                            <li><a href="#">Drop 1</a></li>
                            <li><a href="#">Drop 2</a></li>
                            <li><a href="#">Drop 3</a></li>
                        </ul>
                    </div>
                </li>
           </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container -->
</nav>  

My attempt at this:

    $( document ).ready(function() {
        var section = $('.av-nav .nav li a:hover');
        var width = section.width();
        if (width < 768)
            section.addClass('nobg');
    });

The CSS:

.nobg {background: none!important;}


What am I doing wrong that my code is not working?

I am using bootstrap with dropdown. My anchor has a background color on hover. But when the dropdown is showing i want the parent containing the dropdown to lose the background color.

My HTML is:

  <nav class="navbar navbar-default av-nav" role="navigation">
     <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
            <span class="sr-only"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav nav nav-tabs">
                <li class="lia li1"><a href="#">Home</a></li>
                <li class="dropdown li2"><a class="dropdown-toggle" data-toggle="dropdown">About</a><span class="nav-arrow"></span>
                    <div class="dropdown-menu">
                        <ul>
                            <li><a href="#">Drop 1</a></li>
                            <li><a href="#">Drop 2</a></li>
                            <li><a href="#">Drop 3</a></li>
                        </ul>
                    </div>
                </li>
           </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container -->
</nav>  

My attempt at this:

    $( document ).ready(function() {
        var section = $('.av-nav .nav li a:hover');
        var width = section.width();
        if (width < 768)
            section.addClass('nobg');
    });

The CSS:

.nobg {background: none!important;}


What am I doing wrong that my code is not working?

Share Improve this question edited May 27, 2015 at 20:23 amphetamachine 30.7k12 gold badges67 silver badges74 bronze badges asked May 27, 2015 at 11:04 Alex ZahirAlex Zahir 9697 gold badges21 silver badges50 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 8

You may use these events provided by bootstrap for dropdowns :

show.bs.dropdown : This event fires immediately when the show instance method is called.

shown.bs.dropdown : This event is fired when the dropdown has been made visible to the user (will wait for CSS transitions, to plete).

hide.bs.dropdown : This event is fired immediately when the hide instance method has been called.

hidden.bs.dropdown : This event is fired when the dropdown has finished being hidden from the user (will wait for CSS transitions, to plete).

Usage :

$('.dropdown').on('show.bs.dropdown', function () {
  // do something…
  // In your case
   var section = $('.av-nav .nav li a:hover');
    var width = section.width();
    if (width < 768){
        section.addClass('nobg');}
})
$('.dropdown').on('hide.bs.dropdown', function () {
      // do something…
      // In your case
       var section = $('.av-nav .nav li a:hover');
        var width = section.width();
        if (width < 768){
            section.removeClass('nobg');}
    })

I guess this will work, you might need to do some changes though.

you also need to check when window size is resize. can you plz try this code.

$( document ).ready(function() {
    var section = $('.av-nav .nav li a:hover');
    var width = section.width();
    if (width < 768){
        section.addClass('nobg');
    }

    $(window).resize(function(){
        // on window resize call function to check window width.
        var section = $('.av-nav .nav li a:hover');
        var width = section.width();
        if (width < 768){
           section.addClass('nobg');
        }
    });

});

Use media queries:

@media screen and (max-width:767px){
  .nobg:focus {background: none!important;}
}

Add the class to your anchor tag.

<a class="nobg"...

Pseudo-selectors a:hover should not work in Jquery selector.

First solution is to use hover or on('hover') to trigger the event.

Second one is to use css @media with max-width 768.

You are missing $, add this and try

    $( document ).ready(function() {
        var section = $('.av-nav .nav li a:hover');
        var width = section.width();
        if (width < 768)
            $(section).addClass('nobg');
    });
发布评论

评论列表(0)

  1. 暂无评论