简体   繁体   English

jQuery悬停/单击功能问题

[英]jQuery Hover/Click function issue

I've created a function to handle some interactions. 我创建了一个函数来处理一些交互。 I'm using the function for Clicks and Hovers. 我正在使用“点击和悬停”功能。 In this instance I'm using it for Hovers. 在这种情况下,我将其用于悬停。 It works great, but with one catch. 它的效果很好,但有一个陷阱。 When you hover off a link and hover back on to it nothing happens. 当您将鼠标悬停在链接上并重新悬停在该链接上时,将不会发生任何事情。 You must first hover onto another link before you can hover back to see the feedback. 您必须先将鼠标悬停在另一个链接上,然后才能将鼠标悬停以查看反馈。 The outcome is the same with clicks. 点击的结果是相同的。 If you click on link. 如果您单击链接。 The feedback is displayed, if you click the same link again, the feedback disappears. 显示反馈,如果再次单击同一链接,则反馈消失。 I've created a JSFiddle for an example of the hovers and the clicks. 我已经创建了一个JSFiddle作为悬停和点击的示例。

Hovers: 悬停:

// Applying Best Practices
$(function(){
function applyingBestPractices(event) {

    // Create Vars
    var clickedClass = $(this).attr('class');
    var examples = $(".applying-best-practices .examples");
    var links = $(".applying-best-practices .nav a");

    // Add Tabindex to all Results
    examples.each(function(index) {
        $(this).attr('tabindex', index)
    });

    // Remove class for all
    examples.removeClass("showIT");
    links.removeClass("nav-select");

    // Adds active class to link 
    $(this).addClass("nav-select");

    // Add Class and Focus
    $("#" + clickedClass).addClass("showIT").focus();
    return false;
}
$(".applying-best-practices .nav a").hover(applyingBestPractices);
});

Clicks: http://jsfiddle.net/xEvav/1/ 点击次数: http//jsfiddle.net/xEvav/1/

// Applying Best Practices
$(function(){
function applyingBestPractices(event) {

    // Create Vars
    var clickedClass = $(this).attr('class');
    var examples = $(".applying-best-practices .examples");
    var links = $(".applying-best-practices .nav a");

    // Add Tabindex to all Results
    examples.each(function(index) {
        $(this).attr('tabindex', index)
    });

    // Remove class for all
    examples.removeClass("showIT");
    links.removeClass("nav-select");

    // Adds active class to link 
    $(this).addClass("nav-select");

    // Add Class and Focus
    $("#" + clickedClass).addClass("showIT").focus();
    return false;
}
$(".applying-best-practices .nav a").click(applyingBestPractices);
});

The reason it does not work is because of 它不起作用的原因是因为

var clickedClass = $(this).attr('class');
...
$(this).addClass("nav-select");

After you added a class nav-select 添加班级nav-select

The next time you click on the nav again, clickedClass will return example1 nav-select because you are trying to get the attribute of class which now became class="example1 nav-select" 下次您再次单击导航时,clickedClass将返回example1 nav-select因为您正尝试获取class的属性,该属性现在变为class="example1 nav-select"

Then this will fail: 然后这将失败:

$("#example1 nav-select").addClass("showIT").focus();

Solution: 解:

http://jsfiddle.net/Azefj/ http://jsfiddle.net/Azefj/

HTML: HTML:

<a href="javascript:void();" alt="example1">example1</a>
<a href="javascript:void();" alt="example2">example2</a>
<a href="javascript:void();" alt="example3">example3</a>
....

JS: JS:

// Create Vars
var clickedClass = $(this).attr('alt');

Here is the solution: 解决方法如下:

$(function(){
    function buildingYourKnowledgeAndSkills(event) {

        if ($(this).hasClass('nav-select')) {
            $(".feedback .examples.showIT").focus();
        }
        else {
            // Create Vars
            var clickedClass = $(this).attr('class');
            var examples = $(".building-your-knowledge-and-skills .examples");
            var links = $(".building-your-knowledge-and-skills .nav a");

            // Add Tabindex to all Results
            examples.each(function(index) {
                $(this).attr('tabindex', index)
            });

            // Remove class for all
            examples.removeClass("showIT");
            links.not(this).removeClass("nav-select");

            // Adds active class to link 
            $(this).addClass("nav-select");
            $("#" + clickedClass).addClass("showIT").focus();
        }
        return false;
    }
    $(".building-your-knowledge-and-skills .nav a").click(buildingYourKnowledgeAndSkills);
});

I just added this if else statement to check if the link has the class('nav-select'). 我刚刚添加了这个if else语句,以检查链接是否具有class('nav-select')。

http://jsfiddle.net/xEvav/48/ http://jsfiddle.net/xEvav/48/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM