单击项目时菜单上的 jQuery removeClass

发布于 2024-12-14 09:44:37 字数 805 浏览 4 评论 0原文

好吧,我有这个功能,当单击联系人时,它会向用户添加一个类,

 $(".contactlink").live("click",function(e){
            if(lastclicked == $(this).attr('href'))
            {
            return false;   
            }
            contactinfo = lastclicked.split('/');
            alert(lastclicked);

            lastclicked = $(this).attr('href');
            contactinfo = $(this).attr('href').split("/");
            $("#friend_"+contactinfo[2]).addClass('active');
            loadcontactinfo(contactinfo[2]);
            //alert( contactinfo[2] );
            return false;                                   
        }); 

所有工作正常,但这部分功能似乎没有将其自身保存到 jquery 中。

contactinfo = lastclicked.split('/');
alert(lastclicked);

因为当我执行“lastclicked”时,它不会提醒我上次单击的 href 链接是什么。

Ok so I have this function that when a contact is clicked it adds a class to the user

 $(".contactlink").live("click",function(e){
            if(lastclicked == $(this).attr('href'))
            {
            return false;   
            }
            contactinfo = lastclicked.split('/');
            alert(lastclicked);

            lastclicked = $(this).attr('href');
            contactinfo = $(this).attr('href').split("/");
            $("#friend_"+contactinfo[2]).addClass('active');
            loadcontactinfo(contactinfo[2]);
            //alert( contactinfo[2] );
            return false;                                   
        }); 

All is working fine, but this part of the function seems not to be saving it self to the jquery.

contactinfo = lastclicked.split('/');
alert(lastclicked);

cos when I do lastclicked it does not alert me what the href link which was click was last.

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

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

发布评论

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

评论(2

陌路黄昏 2024-12-21 09:44:37

您应该确保在正确的范围内使用 var 声明变量。尝试类似的操作:

// declare lastclicked before created your click function
var lastclicked = '';
$(".contactlink").live("click",function(e){
    if(lastclicked == $(this).attr('href')) {
       return false;   
    }
    alert(lastclicked);
    // use var for local variables
    var contactinfo = $(this).attr('href').split("/");
    lastclicked = $(this).attr('href');

    $("#friend_"+contactinfo[2]).addClass('active');
    loadcontactinfo(contactinfo[2]);
    //alert( contactinfo[2] );
    return false;                                   
}); 

有关简化的示例,请参阅 this jsfiddle

You should make sure that you are declaring variables with var in the proper scope. Try something like:

// declare lastclicked before created your click function
var lastclicked = '';
$(".contactlink").live("click",function(e){
    if(lastclicked == $(this).attr('href')) {
       return false;   
    }
    alert(lastclicked);
    // use var for local variables
    var contactinfo = $(this).attr('href').split("/");
    lastclicked = $(this).attr('href');

    $("#friend_"+contactinfo[2]).addClass('active');
    loadcontactinfo(contactinfo[2]);
    //alert( contactinfo[2] );
    return false;                                   
}); 

For a simplified example see this jsfiddle

裂开嘴轻声笑有多痛 2024-12-21 09:44:37

您是否在函数之前将 lastclicked 定义为 var

Did you define lastclicked as a var before your function?

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