如何让 div 出现在鼠标悬停在其上的元素旁边? (jQuery)

发布于 2024-11-27 20:40:50 字数 862 浏览 0 评论 0 原文

我有一堆包含在链接中的图像,例如:

<a class="tipsFeatured" href="#" rel="products/p0.html"><img src="assets/img/products-shoppingview-and-registryview/horne/resized/pott-5pc-setting-pott.jpg" alt="Silver Cutlery" width="170" height="170" /></a>
            <a class="tipsFeatured" href="#" rel="products/p1.html"><img src="assets/img/products-shoppingview-and-registryview/horne/resized/Design-House-Stockholm-Cobalt_Pitcher.jpg" alt="Design House Stockholm Cobalt Pitcher" width="170" height="170" /></a>

我有一个 id 为 #descText 的 div,我希望当鼠标悬停在每个链接上时,div 会出现在当前元素旁边,每个链接具有不同的文本时间。 “每次不同的文本”部分我知道该怎么做,但我无法使 div 出现在我刚刚拥有的当前悬停元素旁边:

$('.tipsFeatured').bind('mouseover',function(){
        var $txt = $('#descText');
        $(this).next().append($txt.css('display','block'));
    });

I have a bunch of images wrapped in links such as these:

<a class="tipsFeatured" href="#" rel="products/p0.html"><img src="assets/img/products-shoppingview-and-registryview/horne/resized/pott-5pc-setting-pott.jpg" alt="Silver Cutlery" width="170" height="170" /></a>
            <a class="tipsFeatured" href="#" rel="products/p1.html"><img src="assets/img/products-shoppingview-and-registryview/horne/resized/Design-House-Stockholm-Cobalt_Pitcher.jpg" alt="Design House Stockholm Cobalt Pitcher" width="170" height="170" /></a>

I have a div with id #descText and I'd like that when the mouse is hovered over each of this links that div appears right next to the current element with different text each time. The "different text each time" part I know how to do, but I haven't been able to make the div appear next to the current hovered element I just have:

$('.tipsFeatured').bind('mouseover',function(){
        var $txt = $('#descText');
        $(this).next().append($txt.css('display','block'));
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

过期情话 2024-12-04 20:40:50

您需要使用 .mouseenter().mouseleave().hover() 需要 2 个函数,否则任何内容都是

以下代码将使 #descText 可见,并将其附加到 中,以便它位于锚点内部,因此具有超链接。如果您不希望使用 超链接,请使用 .after() 而不是 .append()

HTML

<a class="tipsFeatured" href="#" rel="products/p0.html"><img src="assets/img/products-shoppingview-and-registryview/horne/resized/pott-5pc-setting-pott.jpg" alt="Silver Cutlery" width="170" height="170" /></a>
<a class="tipsFeatured" href="#" rel="products/p1.html"><img src="assets/img/products-shoppingview-and-registryview/horne/resized/Design-House-Stockholm-Cobalt_Pitcher.jpg" alt="Design House Stockholm Cobalt Pitcher" width="170" height="170" /></a>

<span id="descText">the text</span>

CSS

#descText { display:none; }

JavaScript

$('.tipsFeatured').mouseenter(function() {
    $(this).append($('#descText').show());
}).mouseleave(function() {
    $('#descText').hide();
});

演示

我假设您有多个图像描述 但在附加之前会以某种方式更改 #descText 内的文本,因为这只适用于一个唯一的 #descText 元素。

You need to use a combination of .mouseenter() and .mouseleave() or .hover() that takes 2 functions otherwise anything being .append()ed or after()ed will be added on every mouse move over the anchor tag. I prefer the explicit mouseenter/leave but it's personal preference since the dual function hover is an alias for the mouseenter/leave.

The following will make the #descText visible and append it to the <a>, so that is inside the anchor and therefore is hyperlinked. If you do not want the <span id="descText"/> hyperlinked use .after() instead of .append().

HTML

<a class="tipsFeatured" href="#" rel="products/p0.html"><img src="assets/img/products-shoppingview-and-registryview/horne/resized/pott-5pc-setting-pott.jpg" alt="Silver Cutlery" width="170" height="170" /></a>
<a class="tipsFeatured" href="#" rel="products/p1.html"><img src="assets/img/products-shoppingview-and-registryview/horne/resized/Design-House-Stockholm-Cobalt_Pitcher.jpg" alt="Design House Stockholm Cobalt Pitcher" width="170" height="170" /></a>

<span id="descText">the text</span>

CSS

#descText { display:none; }

JavaScript

$('.tipsFeatured').mouseenter(function() {
    $(this).append($('#descText').show());
}).mouseleave(function() {
    $('#descText').hide();
});

Demo

I assume that you have multiple image descriptions though, and are somehow changing the text inside the #descText before appending, as this only works with one unique #descText element.

总以为 2024-12-04 20:40:50

这应该适合你:

$('.tipsFeatured').hover(
    function(){
        var element = $(this).children(),
            text    = element.attr('data');

    $(this).append('<div id="descText">'+text+'</div>');

    },
    function(){

    $('#descText').remove();

    }
);

演示: http://jsfiddle.net/hx8Tv/ 2/

替换为您的图像。

一点额外

This should do it for you :

$('.tipsFeatured').hover(
    function(){
        var element = $(this).children(),
            text    = element.attr('data');

    $(this).append('<div id="descText">'+text+'</div>');

    },
    function(){

    $('#descText').remove();

    }
);

Demo : http://jsfiddle.net/hx8Tv/2/

Replace <span> with you images.

A little extra.

小猫一只 2024-12-04 20:40:50

解决此问题的一种方法是,您可以在您想要将描述悬停在每个元素旁边创建一个空的

并确保每个 id 都是唯一的。然后在 JavaScript 中,您可以输入类似 $('foo_'+id) 的内容,并通过 id 变量对其进行标识。

One way to go about this is that you can make an empty <div id="foo_1"></div> next to each element that you'd like to have feature a description hoverover and make sure each id is unique. Then in the javascript, you can say something like $('foo_'+id) and have it identified by the id variable.

浅忆 2024-12-04 20:40:50

通过使用 .next(),您可以选择 DOM 中的下一个 a 元素并向其附加描述 div,而不是在当前 之后插入描述 div一个 元素。

试试这个:

$('.tipsFeatured').bind('mouseover',function(){
    var $txt = $('#descText');
    $(this).after($txt.css('display','block'));
});

它使用 jQuery 的 after() 函数。

By using .next(), you are selecting the next a element in the DOM and appending the description div to it instead of inserting the description div after the current a element.

Try this:

$('.tipsFeatured').bind('mouseover',function(){
    var $txt = $('#descText');
    $(this).after($txt.css('display','block'));
});

which uses jQuery's after() function.

凝望流年 2024-12-04 20:40:50

难道不应该是:

$('.tipsFeatured').bind('mouseover',function(){
    var $txt = $('#descText');
    $txt.css('display','block');
    $(this).after($txt);
});

编辑:

更新了我的答案,这就是我回到页面要做的事情。一个很好的教训 - 最好先测试,然后发布,而不是发布并纠正。

Shouldn't that be:

$('.tipsFeatured').bind('mouseover',function(){
    var $txt = $('#descText');
    $txt.css('display','block');
    $(this).after($txt);
});

EDIT:

Updated my answer, which is what I was coming back to the page to do anyway. A good lesson - better to test first, then post, than to post and correct.

×纯※雪 2024-12-04 20:40:50

试试这个

$('.tipsFeatured').hover(function() {
    var contentInsideElement = /* 'Whatever'; */
    var descText =  $("#descText");
      if(descText.length)
        descText.html(contentInsideElement);
      else
         descText = $('<div id="descText">' + contentInsideElement +   '</div>').appendTo(document.body);

    $(this).after(descText);
   }, function() { // Remove new element on mouseout
     $('#descText').hide();
});

Try this

$('.tipsFeatured').hover(function() {
    var contentInsideElement = /* 'Whatever'; */
    var descText =  $("#descText");
      if(descText.length)
        descText.html(contentInsideElement);
      else
         descText = $('<div id="descText">' + contentInsideElement +   '</div>').appendTo(document.body);

    $(this).after(descText);
   }, function() { // Remove new element on mouseout
     $('#descText').hide();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文