2014-12-02 21 views
0

在我以前的post中,我希望它找到特定的dom元素。解決方案中提供的使用find的jquery選擇器非常棒。但我想委託這個選擇器將其與「contextmenu」事件綁定。但是如果你在委託中傳遞jquery對象,它將不起作用。我做的是以下。當使用代表綁定時訪問jquery對象

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)); 
    } 
}); 

我想$這對象是槽,但我看書上說我不能使用jQuery對象中的「上」,但我需要使用一個選擇。什麼是這個等價的選擇器?我想要的td是第二個孩子從所需的tr。它是

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

是的....你的選擇器似乎是正確的 – 2014-12-02 11:41:12

+1

你沒有嘗試之前詢問? – 2014-12-02 12:02:42

+0

我做了沒有工作,它只返回一個結果。我不得不使用第n個孩子作爲波紋管 – Apostolos 2014-12-02 13:04:35

回答

0

on第二個參數應該是一個串選擇中找到此主選擇的後代元素。考慮到這一點,這應該工作:

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

這是行得通嗎?

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

我使用這個,但使用:nth-​​child(2)代替。這工作。謝謝你們! – Apostolos 2014-12-02 13:05:12

+0

如果能解決您的問題,請接受問題。謝謝 – kapantzak 2014-12-02 13:11:07

0

工作原理如下。

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)); 
    } 
});