【问题标题】:Need class to appear when hovering a seperate div悬停单独的 div 时需要显示类
【发布时间】:2020-06-12 08:00:42
【问题描述】:

我有一个 wordpress 网站,上面有一排类似图标的应用程序。当您将每个按钮悬停时,描述应出现在创建的区域下方。由于它们不在同一个 div 中,因此我需要使用 JavaScript 才能使其正常工作,但我已经尝试了所有方法,但没有任何效果。

我将这个添加到 css 中,该类是要隐藏的文本。

#installation-popup {
    display: none;
}

并尝试了多种 Java 方法来显示它,包括以下 3 种方法:

var element = document.getElementsByClassName('.hvr-grow')[0];

element.addEventListener("mouseover",function(){
    document.getElementsByClassName('#installation-popup')[0].style.display = "block";
});

element.addEventListener("mouseout",function(){
    document.getElementsByClassName('#installation-popup')[0].style.display = "none";
});


$(".hvr-grow").hover(
  function() {
    $('#installation-popup').show()
  },
  function() {
    $('#installation-popup').hide()
  }
);

$(document).ready(function () {
    $(document).on('mouseenter', '.hvr-grow', function () {
        $(this).find("#installation-popup").show();
    }).on('mouseleave', '.hvr-grow', function () {
        $(this).find("#installation-popup").hide();
    }); 
});

我该怎么做?

【问题讨论】:

  • 你能分享你的 HTML 吗?我想我在你的 js 中看到了一些问题,但我需要看看你的 HTML 以确保
  • 请制作一个 sn-p、一个小提琴或一个沙盒。
  • 好吧,我以前没用过这个,我试着像上面的网站一样设置它:jsfiddle.net/SamD85/aqvofp0h/1/#&togetherjs=0dkPsTd6nJ

标签: javascript jquery hover


【解决方案1】:

问题是您的选择器错误。使用document.getElementsByClassName 时,不应包含.getElementById 也是一样,你不应该包含 #。这段代码应该适合你:

for(let elem of document.getElementsByClassName('hvr-grow')) {
    elem.addEventListener('mouseover', () => {
        document.getElementById('installation-popup').style.display = 'block';
    });

    elem.addEventListener('mouseout', () => {
        document.getElementById('installation-popup').style.display = 'none';
    });
}    

它遍历所有带有hvr-grow 类的图标,并为每个图标添加事件监听器。我在你的网站上试过了,效果很好。

【讨论】:

  • 我已将您的代码复制到我的主题选项中 - 标头 JavaScript 代码,但它仍然对我不起作用。我知道目前所有图标都是同一类。只是试图让一个工作,然后将改变这些。
  • @Sam'D-Drop'Dines 尝试将此代码放入 $(document).ready 中。如果您没有,请添加它
  • 我像这样添加了它,但什么也没有: $(document).ready(function() { for(let elem of document.getElementsByClassName('hvr-grow')) { elem.addEventListener('mouseover ', () => { document.getElementById('installation-popup').style.display = 'block'; }); elem.addEventListener('mouseout', () => { document.getElementById('installation-popup ').style.display = 'none'; }); } });
  • 可能有什么东西阻止它正常运行。
【解决方案2】:

您可以使用closest 查找与img 最接近的类,然后查找tooltop 以显示或隐藏。

演示代码

$(document).ready(function() {
  $(document).on('mouseenter', '.hvr-grow', function() {
 //img->closest div->find tooltop
   $(this).closest(".kc-col-container").find("div#tooltop").show();
  }).on('mouseleave', '.hvr-grow', function() {
   $(this).closest(".kc-col-container").find("div#tooltop").hide();
  });
});
img{
height:20px;
width:20px;
}
#tooltop {
    display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="icon-navigation" class="kc-elm kc-css-556112 kc_row">
  <div class="kc-row-container  kc-container  navigation">
    <div class="kc-wrap-columns">
      <div class="kc-elm kc-css-671108 kc_col-sm-12 kc_column kc_col-sm-12">
        <div class="kc-col-container">
          <div class="kc-elm kc-css-405743 kc_row kc_row_inner">
            <div id="icon1" class="kc-elm kc-css-400695 kc_col-sm-2 kc_column_inner kc_col-sm-2">
              <div class="kc_wrapper kc-col-inner-container">
                <div class="kc-elm kc-css-84984 kc_shortcode kc_single_image">

                  <a href="#" title="" target="_self">
                    <img src="http://shortcircuit.plexsitesprojects.com/wp-content/uploads/2020/06/Installation-Booking.png" class="hvr-grow" alt="" /> </a>
                </div>
              </div>
            </div>
            <div class="kc-elm kc-css-611650 kc_col-sm-2 kc_column_inner kc_col-sm-2">
              <div class="kc_wrapper kc-col-inner-container">
                <div class="kc-elm kc-css-953031 kc_shortcode kc_single_image">

                  <a href="#" title="" target="_self">
                    <img src="http://shortcircuit.plexsitesprojects.com/wp-content/uploads/2020/06/Renew-subscription.png" class="hvr-grow" alt="" /> </a>
                </div>
              </div>
            </div>
            <div class="kc-elm kc-css-970768 kc_col-sm-2 kc_column_inner kc_col-sm-2">
              <div class="kc_wrapper kc-col-inner-container">
                <div class="kc-elm kc-css-574150 kc_shortcode kc_single_image">

                  <a href="#" title="" target="_self">
                    <img src="http://shortcircuit.plexsitesprojects.com/wp-content/uploads/2020/06/Hotspot-Zone.png" class="hvr-grow" alt="" /> </a>
                </div>
              </div>
            </div>
            <div class="kc-elm kc-css-173984 kc_col-sm-2 kc_column_inner kc_col-sm-2">
              <div class="kc_wrapper kc-col-inner-container">
                <div class="kc-elm kc-css-893340 kc_shortcode kc_single_image">

                  <a href="#" title="" target="_self">
                    <img src="http://shortcircuit.plexsitesprojects.com/wp-content/uploads/2020/06/Security-Installation.png" class="hvr-grow" alt="" /> </a>
                </div>
              </div>
            </div>
            <div class="kc-elm kc-css-333215 kc_col-sm-2 kc_column_inner kc_col-sm-2">
              <div class="kc_wrapper kc-col-inner-container">
                <div class="kc-elm kc-css-901104 kc_shortcode kc_single_image">

                  <a href="#" title="" target="_self">
                    <img src="http://shortcircuit.plexsitesprojects.com/wp-content/uploads/2020/06/Internet-Time.png" class="hvr-grow" alt="" /> </a>
                </div>
              </div>
            </div>
            <div class="kc-elm kc-css-258048 kc_col-sm-2 kc_column_inner kc_col-sm-2">
              <div class="kc_wrapper kc-col-inner-container" value="abc">
                <div class="kc-elm kc-css-651959 kc_shortcode kc_single_image" value="abc">

                  <a href="#" title="" target="_self" value="abc">
                    <img src="http://shortcircuit.plexsitesprojects.com/wp-content/uploads/2020/06/Custom-Payments.png" class="hvr-grow" alt="" /> </a>
                </div>
              </div>
            </div>
          </div>
          <div id="tooltop" class="kc-elm kc-css-489664 kc_row kc_row_inner" >
            <div class="kc-elm kc-css-878424 kc_col-sm-12 kc_column_inner kc_col-sm-12">
              <div class="kc_wrapper kc-col-inner-container">
                <div class="kc-elm kc-css-800878 kc_text_block installation-booking-text">
                  <p>
                    <span id="installation-popup" style="color: #fff;"><strong>Tooltip:</strong></span> <span id="installation-popup" style="color: #cccccc;">        Internet installation within 24 working hours. No telephone line required.</span></p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

【讨论】:

  • 当我将它添加到网站时,它似乎也不起作用。
  • 您的控制台是否显示任何错误?您提供的html代码也相同吗?因为它在上面的演示代码中运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多