Jquery追加到某处,然后将其移回
我遇到了一些工具提示之类的问题。我在列表项中有一个隐藏的 div,我想在鼠标悬停时显示它。问题是列表是一个轮播,因此如果它是最后一项,提示将在溢出后面丢失。
我的解决方案是将 div 移到外面,效果很好,并且按照我喜欢的方式显示。但我不知道如何把它放回去。 div 中会有链接,所以我需要能够将鼠标悬停在它上面。
这是我的意思的一个简单版本:
$('.wrapper li').mouseover(function() {
$(this).children('.This_is_hidden').clone().appendTo(".other").css('display', 'block' );
}
});
$('.wrapper li').mouseout(function() {
// i want to put it back in the same li
}
});
这是标记:
<div class="wrapper">
<ul>
<li><a href="" title="Tall Glow"><img src="images/thumb1.jpg" height="80" width="80" alt="Tall Glow" /></a>
<div class="This_is_hidden">stuff that I want to move</div>
</li>
<li><a href="" title="Tall Glow"><img src="images/thumb1.jpg" height="80" width="80" alt="Tall Glow" /></a>
<div class="This_is_hidden">stuff that I want to move</div>
</li>
<li><a href="" title="Tall Glow"><img src="images/thumb1.jpg" height="80" width="80" alt="Tall Glow" /></a>
<div class="This_is_hidden">stuff that I want to move</div>
</li>
</ul>
</div>
<div class="other">
want to put the div here
</div>
任何帮助将不胜感激。
I have sort of a tool tip kind of thing that I'm having trouble with. I have a hidden div in a list item, that I want to reveal on mouse over. The problem is the list is a carousel, so the tip will get lost behind the overflow if it is the last item.
My solution was to move the div outside, which work just fine, and displays as i like. But i'm having trouble figuring out how to put it back. The div will have links in it, so i need to be able to hover over it.
Here is a simple version of what I mean:
$('.wrapper li').mouseover(function() {
$(this).children('.This_is_hidden').clone().appendTo(".other").css('display', 'block' );
}
});
$('.wrapper li').mouseout(function() {
// i want to put it back in the same li
}
});
here is the markuo:
<div class="wrapper">
<ul>
<li><a href="" title="Tall Glow"><img src="images/thumb1.jpg" height="80" width="80" alt="Tall Glow" /></a>
<div class="This_is_hidden">stuff that I want to move</div>
</li>
<li><a href="" title="Tall Glow"><img src="images/thumb1.jpg" height="80" width="80" alt="Tall Glow" /></a>
<div class="This_is_hidden">stuff that I want to move</div>
</li>
<li><a href="" title="Tall Glow"><img src="images/thumb1.jpg" height="80" width="80" alt="Tall Glow" /></a>
<div class="This_is_hidden">stuff that I want to move</div>
</li>
</ul>
</div>
<div class="other">
want to put the div here
</div>
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为移动的锚点分配一个临时类。然后,当您
鼠标移出
时,将具有临时类的元素添加到该元素,并删除临时类。由于
mouseout
事件始终发生在mouseover
事件之前,因此一个唯一的临时类名足以处理所有元素。Assign a temporary class to the moved anchor. Then, when you
mouseout
, add the element with the temporary class to the element, and remove the temporary class.Because the
mouseout
event always happens before themouseover
event, one unique temporary class name is enough to deal with all elements.解决此问题的一个更好的解决方案是使用特制的 jQuery
hover()
函数,该函数旨在处理 Enter & 。保留函数,并在 HTML5 中提供编码,利用新的 data-* 自定义属性来包含“工具提示”内容。首先,正如我上面提到的,将
hover()
函数绑定到包装器列表项,如下所示:您可以找到
hover()
函数的完整文档 < a href="http://api.jquery.com/hover/" rel="nofollow">位于 jQuery API 站点。然后在 HTML 标记中,通过添加以下内容来使用 data-* 属性:
data-tooltip-content=""
到列表项内的锚点 - 这看起来像:支持新 data-* 自定义属性的文档可以在此 html5doctor 网站 上找到。
现在,您可以在两个处理程序之间添加更多 jQuery 函数,以向网站添加新元素以显示工具提示内容。这可以通过使用 jQuery 函数来完成,但是,需要先将该元素附加到页面,然后才能使用 DOM 访问它。这是在页面中添加和删除它的方法:
您现在可以使用类来自定义工具提示以进行相应的定位。除了将新元素附加到正文之外,您实际上可以将其附加到页面上的特定位置,即
.appendTo('.wrapper')
。使用我上面解释的代码查看 this JSfiddle ,我添加了一些样式来制作东西更容易看到。希望这有帮助。
A better solution to this problem would be to make use of the purpose made jQuery
hover()
function, which is designed to handle the enter & leave functions, and, providing your coding in HTML5, make use of the new data-* custom attributes to contain your "tooltip" content.Firstly, as i've mentioned above bind the
hover()
function to your wrapper list items, like this:You can find the full documentation for the
hover()
function here on the jQuery API site.Then in your HTML markup, make use of the data-* attribute by adding something along the lines of:
data-tooltip-content=""
to the anchors within the list items - this would look something like:The documentation supporting the new data-* custom attributes can be found on this html5doctor website.
You can now add in some more jQuery functions between the two handlers to add a new element to the website to show the tooltip content. This can be done by using a jQuery function, however, this element needs to be appended to the page before you can access it using the DOM. This is how you'd add and remove it from the page:
You can now customise the tooltip using the class to position accordingly. Alternativly to appending the new element to the body, you could infact append it to a specific place on your page, i.e.
.appendTo('.wrapper')
.Check out this JSfiddle with the code i've explained above, i've added a bit of styling to make things easier to see. Hope this helps.