简体   繁体   中英

Access jquery object when binding it using delegates

In my previous post I want it to find specific dom elements. The jquery selector using find provided in the solution was great. But I want to delegate this selector to bind it with "contextmenu" event. But it won't work if you pass jquery object in the delegate. What I do is the following.

var slots;
slots = $(".fc-slats > table tbody tr ").find("td:eq(1)");
$(".fc-agenda-view").on("contextmenu", slots, function (e){
    e.preventDefault();
    if (paste===true) {
        showSlotContextualMenu($(this), e);
    }else{
        console.log($(this));
    }
});

I want $this object to be the slot but I read that I cannot use jquery object in "on" but I need to use a selector. What would be the equivalent selector for this?I want the td that is second child from the desired tr. Is it

.fc-slats > table tbody tr td:eq(1)

The second parameter of on should be a string selector to find the descendant elements of the primary selector. With that in mind, this should work:

$(".fc-agenda-view").on("contextmenu", '.fc-slats > table tbody tr td:eq(1)', function(e) {
    // your code...
});

Does this work?

$(document).on("contextmenu", ".fc-slats > table tbody tr > td:first-child", function (e){
     // .....
});

What worked was the following.

var selector = ".fc-slats > table tbody tr td:nth-child(2)";
$(".fc-agenda-view").on("contextmenu",selector , function (e){
    e.preventDefault();
    if (paste===true) {
        showSlotContextualMenu($(this), e);
   }else{
        console.log($(this));
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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