jquery 元素的单独 mouseenter

发布于 2024-10-03 10:53:34 字数 1011 浏览 1 评论 0原文

我正在编写“向上、向下”投票脚本。例如,当用户将鼠标悬停在“向上”按钮上时,工具提示应淡入相应按钮上并显示“您喜欢这篇文章”或诸如此类的内容。然而,工具提示会在所有按钮上淡入。

脚本较长,但这是工具提示部分。

$(document).ready(function() {
$('.vote').mouseenter(function(e) {
        var vote_status = $(this).attr("name");
        $('.tooltip').fadeIn(200);
        if( vote_status = "up" ) {
            $('.tooltip').html('You like this post');
        } 
        if ( vote_status = "down" ) {
            $('.vp_tooltip').html('You dislike this post');
        }
})
.mouseleave(function(e) {
        $('.tooltip').fadeOut(200);
});                                                                        
});

HTML..

<div class="tooltip"></div>
<a name="up" class="vote" id="<?php the_ID(); ?>">Up</a>

<div class="tooltip"></div>
<a name="down" class="vote" id="<?php the_ID(); ?>">Down</a>

此外,由于某种原因,它不会识别您是否投票“赞成”或“反对”。不管怎样,在 mouseenter 上它都会显示“你不喜欢这篇文章”。不确定我到底做错了什么。

I'm working on a "up, down" voting script. When the user for example hovers over the "Up" button a tooltip should fadeIn over that respective button and say "You like this post" or whatnot. The tooltip however fades in on all buttons..

The script is longer but here is the tooltip part.

$(document).ready(function() {
$('.vote').mouseenter(function(e) {
        var vote_status = $(this).attr("name");
        $('.tooltip').fadeIn(200);
        if( vote_status = "up" ) {
            $('.tooltip').html('You like this post');
        } 
        if ( vote_status = "down" ) {
            $('.vp_tooltip').html('You dislike this post');
        }
})
.mouseleave(function(e) {
        $('.tooltip').fadeOut(200);
});                                                                        
});

HTML..

<div class="tooltip"></div>
<a name="up" class="vote" id="<?php the_ID(); ?>">Up</a>

<div class="tooltip"></div>
<a name="down" class="vote" id="<?php the_ID(); ?>">Down</a>

Also for some reason it doesn't pick up whether you voted "up" or "down". On mouseenter it displays "You dislike this post" regardless. Not sure what exactly I'm doing wrong.

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

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

发布评论

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

评论(3

飘逸的'云 2024-10-10 10:53:34

.tooltip 适用于两个工具提示,而不仅仅是您想要的工具提示。因此,当鼠标指针进入/离开其中一个工具提示时,您会同时淡入/淡出两个工具提示。如果 为赞成票和反对票生成相同的 ID,您将拥有重复的 ID,这在 HTML 中无效,并且会导致问题。

我强烈建议使用 HTML 类而不是 name 属性。您是否知道单个元素上可以有多个类?只需用空格分隔它们(例如 class="vote up")。

如果页面上有多个投票按钮,我还会将所有投票按钮包装在一个容器 div 中(将它们分组在一起)。 (这样,您可以仅将 ID 赋予一个元素并使用 .parent().attr('id') 访问它。)

<div class="votePanel">
    <div class="tooltip"></div>
    <a class="vote up">Up</a>

    <div class="tooltip"></div>
    <a class="vote down">Down</a>
</div>

这样您的 JavaScript 代码就可以检查这些类是否存在。请注意,在下面的代码示例中, .prev() 指的是之前的元素:

编辑:我想出了一个很好的演示页面展示了这些类对于 CSS 样式也有多么有用。

$(document).ready(function() {
    $('.vote')
        .mouseenter(function(e) {
            var vote = $(this),
                tooltip = vote.prev();

            tooltip.fadeIn(200);
            if(vote.hasClass('up')) tooltip.html('You like this post');
            if(vote.hasClass('down')) tooltip.html('You dislike this post');
        })
        .mouseleave(function(e) {
            $(this).prev().fadeOut(200);
        });
});

.tooltip applies to both tooltips, not just the one you want. So you are fading in/out both tooltips at the same time when the mouse pointer enters/leaves just one of them. And if <?php the_ID(); ?> generates the same ID for both the upvote and the downvote, you would have duplicate ID's, which is not valid in HTML and will lead to problems.

I strongly recommend using HTML classes rather than name attributes. Did you know that you can have multiple classes on a single element? Just separate them by spaces (e.g. class="vote up").

I would also wrap all the vote buttons in a container div (to group them together) if there will be more than one set on a page. (That way, you can give the ID to only one element and access it using .parent().attr('id').)

<div class="votePanel">
    <div class="tooltip"></div>
    <a class="vote up">Up</a>

    <div class="tooltip"></div>
    <a class="vote down">Down</a>
</div>

So then your JavaScript code could check for the existence of these classes. Note that in the below code example, .prev() refers to the element immediately before:

EDIT: I came up with a good demo page that shows how useful the classes can be to CSS styles as well.

$(document).ready(function() {
    $('.vote')
        .mouseenter(function(e) {
            var vote = $(this),
                tooltip = vote.prev();

            tooltip.fadeIn(200);
            if(vote.hasClass('up')) tooltip.html('You like this post');
            if(vote.hasClass('down')) tooltip.html('You dislike this post');
        })
        .mouseleave(function(e) {
            $(this).prev().fadeOut(200);
        });
});
佼人 2024-10-10 10:53:34

这一行:

$('.tooltip').fadeIn(200);

正在选择两个 div。例如,您可以添加一个 id 属性并像这样找到它们 - 这是一种方法:

    if( vote_status = "up" ) {
        $('#tooltip_up').fadeIn(200);
        $('.tooltip').html('You like this post');
    } 

<div id="tooltip_up" class="tooltip"></div>
<a name="up" class="vote" id="<?php the_ID(); ?>">Up</a>

This line:

$('.tooltip').fadeIn(200);

is selecting both divs. You can e.g add an id attribute and find them like that - here's one way:

    if( vote_status = "up" ) {
        $('#tooltip_up').fadeIn(200);
        $('.tooltip').html('You like this post');
    } 

<div id="tooltip_up" class="tooltip"></div>
<a name="up" class="vote" id="<?php the_ID(); ?>">Up</a>
↙厌世 2024-10-10 10:53:34

当谈论当前的工具提示时,您可以使用 jquery prev() 函数来获取前一个节点...因此:变成

$('.tooltip').fadeIn(200);

$(this).prev().fadeIn(200);

您也可以将其存储在变量中,因为您使用了两次:

var tooltip = $(this).prev()

编辑

这是 jsfiddle 的示例

When talking about the current tool tip you can use the jquery prev() function to get the previous node... so this:

$('.tooltip').fadeIn(200);

becomes:

$(this).prev().fadeIn(200);

You may as well store that in a variable since you use it twice:

var tooltip = $(this).prev()

EDIT

Here is an example on jsfiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文