2012-04-04 64 views
2

我已經建立了它看起來像這樣的功能jQuery選擇:jQuery選擇。不是()不工作

$('html').not('.table-main tr[selected]').mousedown(function(e) { 

但不知何故,它不是在所有的過濾,我不明白爲什麼。 即使我只是離開('.table-main')對於選擇器,我仍然在點擊表格時觸發該函數......這是什麼問題?

使用文件「身體」,而不是「HTML」沒有幫助,因爲文件是不是在所有。不觸發()和「體」的結果相同。

編輯: 剛剛嘗試使用表格外的另一個塊,它的工作原理。有沒有機會我不能在桌上使用它?

UPDATE2:按照要求,這裏是表的渲染HTML代碼:

<table border="0" cellspacing="2px" cellpadding="5px" class="table-main"> 
     <tr id="tablefields"> 
     <th style="padding: 0;"> 
      <table cellpadding="0" cellspacing="0" border="0" align="center"> 
      <tr><th><div class="tr-head-get" id="tr-get-0">Name</div></th></tr> 
      <tr><th><div class="th-header" id="th-header-0" style="top: 54px;">Name</div></th></tr> 
      </table> 
     </th>  
     <th style="padding: 0;"></th> 
    </tr> 
     <tr id='table_form_new_device' class='table_form_class'> 
     <form accept-charset="UTF-8" action="/devices" class="new_device" data-remote="true" id="new_device" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="T2dSeZpxxvgJdTP+yoE7BrVODzqJ95gyVu9Wh/v7oP8=" /></div> 
     <th class='tablefield'> 
      <input id="device_devicename" name="device[devicename]" size="10" type="text" /> 
     </th> 
     <th> 
      <div class='submitbox' id='submitbox_new_device'>S</div> 
      <script> 
      $('#submitbox_new_device').click(function (event) { 
       $('#new_device').submit(); 
      }); 
      </script> 
     </th> 
    </form></tr> 
    </table> 
    <div class="dropdown-button" style="margin: 0 2px;">Filter</div> 
+1

此外,它可以是事件冒泡問題。閱讀更多關於事件順序的信息http://www.quirksmode.org/js/events_order.html – antyrat 2012-04-04 17:04:02

+0

所以你試圖選擇每個未被選中的tr?看看[選擇]選擇器的[jQuery文檔](http://api.jquery.com/selected-selector/)。你的語法看起來有點偏離。但是,一些DOM示例會幫助您更好地幫助您。 – Primus202 2012-04-04 17:04:42

回答

3

你真的想使用委託的事件處理,是因爲你不想附加事件處理程序到文檔中的每個單個元素。您想要在文檔級別攔截冒泡事件,並檢查mousedown是否來自表中不是選定行的元素。

此外tr[selected]似乎不太可能工作。我認爲您需要將類"selected"添加到所選行,然後使用tr.selected作爲選擇器。

如果你做出的改變,我建議像這兩個選項之一:

$(document).mousedown(function(e) { 
    if (!$(e.target).is('.table-main tr.selected')) { 
     // mouse down and it wasn't in a selected row of your table) 
    } 
}) 

您可能還可以讓jQuery的代表團做更多的工作:

$(document).on('mousedown', ':not(.table-main tr.selected)', function() { 
    // mouse down and it wasn't in a selected row of your table) 

}); 

看到您的實際HTML後,如果我正確理解問題,這應該工作。這會給你一個事件隨時點擊情況,只要是點擊是不連續的table-main有它的selected類:

$(document).on('mousedown', function(e) { 
    // mouse down and it wasn't in a selected row of your table) 
    e.stopPropagation(); 
    if (!$(e.target).closest(".table-main tr.selected").length) { 
     console.log("mousedown not in selected row of table-main") 
    } 
});​ 

這裏有一個演示:http://jsfiddle.net/jfriend00/5QD6N/

+0

作出反應。這實際上起作用了(不知道爲什麼)如果我使用**。table-main ** only並單擊細胞。不,如果我直接點擊單元格。我是否必須將表內的每個類都分配給選擇器? – Nikom 2012-04-04 17:40:04

+0

將表格的實際HTML添加到您的問題中,我們可以提供更具體的幫助。您還需要更具體地確定您執行哪些點擊操作,而不希望在此處理程序中處理。這使用事件冒泡和事件委託來優雅地解決問題。另外,您嘗試了兩種選擇中的哪一種? – jfriend00 2012-04-04 17:42:55

+0

以較短的形式發佈呈現的html,因爲真正的表格像5英里長 – Nikom 2012-04-04 17:59:12

2

使用[selected]使得tr類型的元素沒有意義。您使用選定的輸入(checkbox,radio)。您可以在.table-main中的tr元素中添加一個類.selected,然後使用:

編輯:像上面發佈的人一樣,您想要使用選擇嵌套在主體內的元素。

$('body').find(':not(.selected)').mousedown(function(e){}) 
+0

實際上它應該是通過.data()分配的布爾值,但我想它是不正確的語法 – Nikom 2012-04-04 17:15:11

1

$('html')創建一個包含您的HTML元素的jQuery對象。

.not('.table-main tr[selected]')刪除所有屬於table-main類成員的元素的所有屬性(此處存在錯誤,該屬性未出現在TR上)的TR元素。由於HTML元素不是TR元素,並且不能從任何東西繼承,所以這不會消除任何元素,也不會產生任何效果。

mousedown(function(e)將事件處理程序綁定到HTML元素。除非停止傳播,否則任何點擊最終都會冒泡到那裏。

猜測(因爲你實際上並沒有說你想達到什麼)要捕獲未選擇在TR元素的點擊次數。假設您切換到使用類,使HTML有效...

$('.table-main tr:not(.selected)').mousedown(…); 

<tr class="selected"> 
+0

我想在這個唯一的表格行中除了mousedown以外的任何地方調用事件。 [selected]應該是通過.data()分配給行的布爾值。 爲了簡化我只能使用表格的事情,但它不起作用。 使用$('html:not(.table-main)')仍然對.table-main – Nikom 2012-04-04 17:20:50