简体   繁体   English

返回正确的元素ID的问题

[英]Trouble with returning a correct element ID

I've got three links which serve as sorting headings for a table, each is a member of the "sort" class. 我有三个链接用作表的排序标题,每个链接都是“ sort”类的成员。 I wrote a simple jquery function that gets triggered when any "sort" class link is clicked. 我编写了一个简单的jquery函数,当单击任何“排序”类链接时都会触发该函数。 There's a switch case that assigns a certain column number (columnNum) to be used with the tablesorter plugin. 有一个开关盒,分配一个特定的列号(columnNum)与tablesorter插件一起使用。 However, right now I'm getting some weird values returned for the id of the sort classes. 但是,现在我得到一些奇怪的值作为排序类的ID返回。

$(".sort").toggle(function() { 
    // the column id's are datecaption, hourscaption, taskcaption
    var column = $(this).attr('id');
    var columnNum;

    switch(column){
        case 'datecaption':
            columnNum = 1;
            break;
        case 'hourscaption':
            columnNum = 2;
            break;
        case 'taskcaption':
            columnNum = 3;
            break;
        default:
            break;
    }

    var sorting = [[columnNum,1]]; 
    $("#task_table").trigger("sorton",[sorting]); 
    return false; 
}, function(){
    var sorting = [[columnNum,0]]; 
    $("#task_table").trigger("sorton",[sorting]); 
    return false;               
});  

If I alert the column variable, I get "aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown"--which doesn't serve the rest of the function well. 如果我提醒列变量,则会收到“ aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown”信息-不能很好地完成其余功能。

Can anyone tell me why this might be happening? 谁能告诉我为什么会这样吗?

Thanks 谢谢

Without more code or the HTML it's hard to say. 没有更多的代码或HTML,很难说。 My guess is $(this) isn't what you think it is. 我的猜测是$(this)不是您想的那样。 Try adding console.log($(this)); 尝试添加console.log($(this)); (preferably in Firebug) click the result and you will see what $(this) is actually referring to. (最好在Firebug中)单击结果,您将看到$(this)实际指的是什么。

Additionally, you could query the DOM for $('#aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown') and see and you should get the element $(this) is referring to. 另外,您可以在DOM中查询$('#aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown')并查看,您应该获得元素$(this)的引用。

For more information on what "this" will refer to in different situations, check out the Mozilla Developer Network: https://developer.mozilla.org/en/JavaScript/Reference/Operators/this 有关在不同情况下将指代“ this”的更多信息,请查看Mozilla开发人员网络: https : //developer.mozilla.org/en/JavaScript/Reference/Operators/this

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

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