繁体   English   中英

将鼠标悬停在一个 div 上时,如何将 class 添加到另一个 div?

[英]How do add a class to another div, when hovering over one div?

I am trying to add class to .sectionmenu div, but for some reason it adds the class to both the div elements when I hover over the .tabone.toggleClass , the div has same the class.

When you hover our first div ie .tabone.toggleClass it should add class to first div.sectionmenu , similarly when you hover our the second div ie .tabone.toggleClass it should add class to second div.sectionmenu .

 (function($) { $(document).ready(function() { $('.tabone.toggleClass').hover(function() { var mine = $(this).closest('.menubox'); $(this).closest('.main-section').find('.sectionmenu').not(mine).removeClass('class_name'); mine.addClass('class_name'); }); }); }(jQuery));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="main-section"> <div id="col1"> <div class="hidden">hidden text</div> </div> <div id="col2" class="class"> <div class="menubox tabone"> --> when hover over this div it should add class to first.sectionmenu div <a class="toggleClass">toggleClass</a> <a class="toggleClass">toggleClass</a> </div> <div class="menubox tabone"> <a class="toggleClass">toggleClass</a> <a class="toggleClass">toggleClass</a> </div> </div> <div id="col3" class="class"> <div class="sectionmenu"> <div class="menubox"> <a class="toggleClass">toggleClass</a> <a class="toggleClass">toggleClass</a> </div> </div> <div class="sectionmenu"> <div class="menubox"> <a class="toggleClass">toggleClass</a> <a class="toggleClass">toggleClass</a> </div> </div> </div> </div>

一种方法如下,这里我们使用 jQuery 添加自定义data-*属性,以便将元素关联在一起:

 // here we select elements with an 'id' attribute that starts with // the string 'col', // we then iterate over that collection of elements using the // each() method: $('[id^=col]').each(function() { // caching variables to avoid - where possible - repeated look-ups // for the same items; // here we cache the current element: let ancestor = $(this), // we then cache the '.menubox' descendants of the // current element: menuboxes = ancestor.find('.menubox'), // we then cache the '.toggleClass' elements: toggleClassLinks = ancestor.find('.toggleClass'); // iterating over the menuboxes collection, and setting the // custom 'data-*' attribute (here named 'data-index'), // to contain the index of the current.menubox element // within the collection, or its index within the current // [id^=col] element: menuboxes.attr('data-index', function(i) { return i; }); // if there are a non-zero number of '.toggleClass' elements: if (toggleClassLinks.length) { // we iterate over that collection and use the on() method // to bind the anonymous function as an event-handler for // the 'mouseenter' event: toggleClassLinks.on('mouseenter', function(e) { // caching the current.toggleClass element: let target = $(this), // caching the various elements: grandparent = target.closest('.main-section'), parent = target.closest('.menubox'), // caching the attribute-value of the 'data-index' // attribute we set earlier, using the data() method // (because of a peculiarity of the method we couldn't // set the attribute using that method, but retrieving // is consistent): index = parent.data('index'); // here we find the elements with the class of 'class_name' within the // ancestor element, and remove that class: grandparent.find('.class_name').removeClass('class_name'); // here we use a template-literal string to interpolate the 'index' // variable into the string, to create an attribute-selector wherein // the index is equal to the index of the currently hovered.toggleClass // element's parent: grandparent.find(`[data-index=${index}]`) // we then filter out the current.toggleClass element's parent: .not(parent) // and add the class_name class to the other remaining element(s) // that matched the original selector: .addClass('class_name'); }); } });
 *, ::before, ::after { box-sizing: border-box; font: 1rem / 1.5 sans-serif; font-weight: normal; margin: 0; padding: 0; } div { border: 1px solid #000; padding: 0.5em; }.main-section { border-color: transparent; display: grid; grid-template-columns: repeat(3, 1fr); gap: 1em; min-height: 50vh; }.main-section>div { display: flex; flex-direction: column; justify-content: space-between; }.class_name, .class_name.toggleClass { color: #f90; transition: color 0.3s ease-in-out; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="main-section"> <div id="col1"> <div class="hidden">hidden text</div> </div> <div id="col2" class="class"> <div class="menubox tabone"> <a href="#" class="toggleClass">toggleClass</a> </div> <div class="menubox tabone"> <a href="#" class="toggleClass">toggleClass</a> </div> </div> <div id="col3" class="class"> <div class="sectionmenu"> <div class="menubox"> <a href="#" class="toggleClass">toggleClass</a> </div> </div> <div class="sectionmenu"> <div class="menubox"> <a href="#" class="toggleClass">toggleClass</a> </div> </div> </div> </div>

JS 小提琴演示

参考:

展示<div>当悬停在另一个 div 及其子项上时</div><div id="text_translate"><p>我有一个 HTML div,里面有孩子。 我有一个附加到 div 的 jQuery 鼠标悬停事件。 鼠标悬停时,我显示另一个 div,鼠标悬停时,我隐藏它。</p><p> 但是,当我将鼠标悬停在“premiumlink”div 上时,一切正常,但是当我将鼠标移到 div 的一个子项上时,有条件显示的 div 会隐藏,但随后 jQuery 发现父 div 仍在悬停,所以它再次显示它。 然后,如果我将 cursor 移回父 div,则该 div 被隐藏然后再次显示。</p><p> 我怎样才能让鼠标悬停和鼠标悬停适用于所有孩子,而不是这个跳跃的 state?</p><p> 这是我的 HTML</p><pre> &lt;div class="platinumlevel" id="premiumlink"&gt; &lt;h1&gt; &lt;img src="~/Content/Images/colorworld.png" alt="Logo" class="eventimage" /&gt; &lt;a href="@Url.Action("Exhibitor1Attendee", "Home")" class="landing"&gt;Company Name&lt;/a&gt; &lt;/h1&gt; &lt;div id="demopreview" style="display: none;"&gt; I should be displayed when "premiumlink" and all it's children are mouseovered &lt;/div&gt; &lt;/div&gt;</pre><p> JS</p><pre> $("#premiumlink").mouseover(function () { $('#demopreview').show(1000); }).mouseout(function () { $('#demopreview').hide(1000); });</pre></div>

[英]Display <div> when hovering over another div and its children

暂无
暂无

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

相关问题 将鼠标悬停在div上时,如何将一个类添加到另一个div? 将鼠标悬停在另一个div上时如何显示div 当div超过另一个div时添加一个类 当我将鼠标悬停在另一个类上时,我可以在div中添加一个类,而不使用Ember中的jQuery 将鼠标悬停在另一个div上时,将其定位到特定div 悬停在一个div上时如何显示更多div? 将鼠标悬停在一个 div 上以触发另一个 div 中的动画 展示<div>当悬停在另一个 div 及其子项上时</div><div id="text_translate"><p>我有一个 HTML div,里面有孩子。 我有一个附加到 div 的 jQuery 鼠标悬停事件。 鼠标悬停时,我显示另一个 div,鼠标悬停时,我隐藏它。</p><p> 但是,当我将鼠标悬停在“premiumlink”div 上时,一切正常,但是当我将鼠标移到 div 的一个子项上时,有条件显示的 div 会隐藏,但随后 jQuery 发现父 div 仍在悬停,所以它再次显示它。 然后,如果我将 cursor 移回父 div,则该 div 被隐藏然后再次显示。</p><p> 我怎样才能让鼠标悬停和鼠标悬停适用于所有孩子,而不是这个跳跃的 state?</p><p> 这是我的 HTML</p><pre> &lt;div class="platinumlevel" id="premiumlink"&gt; &lt;h1&gt; &lt;img src="~/Content/Images/colorworld.png" alt="Logo" class="eventimage" /&gt; &lt;a href="@Url.Action("Exhibitor1Attendee", "Home")" class="landing"&gt;Company Name&lt;/a&gt; &lt;/h1&gt; &lt;div id="demopreview" style="display: none;"&gt; I should be displayed when "premiumlink" and all it's children are mouseovered &lt;/div&gt; &lt;/div&gt;</pre><p> JS</p><pre> $("#premiumlink").mouseover(function () { $('#demopreview').show(1000); }).mouseout(function () { $('#demopreview').hide(1000); });</pre></div> 将鼠标悬停在另一类上时如何更改它的CSS? 如何通过使用jQuery将鼠标悬停在另一个div上来滑动一个div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM