2012-04-30 61 views
5

我有所有具有相同類的div列表,我想對所有這些不是點擊的應用函數(this),我如何選擇!this與jQuery?如何選擇不是'this'的元素?

更新: 我做了這個,它不工作,任何想法爲什麼?

$("li").each(function(){ 
     $("li").not(this).click(function(e) { 
      $(this).hide(); 
     }); 
    }); 

更新2:這是整個實際代碼:

$(".mark").click(function(e) { 
    e.preventDefault(); 
    var id = "#" + $(this).parent().parent().parent().parent().attr("id") + " "; 

    var currentStatus = "deleted"; // to be replaced with actual status 
    var currentStatusClass = "." + currentStatus + "-heading"; 
    $(id + currentStatusClass).show(); 

    $(id + ".edit-headings").click(function(){ 
     $(this).find(".headings-status").show().addClass("bg-hover"); 

     $(id + ".headings-status").click(function() { 
      $(id + ".headings-status").not(this).hide(); 
     }); 
    }); 
}); 
+0

它不工作,因爲你說:'李(每個),而不是李 - > hide' –

+0

是'this'這裏包括了所有的' li'元素?不只是點擊一個?那麼我怎麼才能得到點擊'li'? – ilyo

+0

什麼是'newStatus'? – thecodeparadox

回答

3

使用.not()功能:

$('.yourclass').click(function() { 
    $('.yourclass').not(this).yourFunc(); 
}); 
7

看一看jQuerys not功能:http://api.jquery.com/not/

$('div').not(this).doSomething(); 

關於你提到的更新,請嘗試:

 $("li").click(function(e) { 
      $("li").not(this).hide(); 
     }); 
+0

我使用類名稱,而不是'li',它不工作,任何想法爲什麼? – ilyo

+0

你可以發佈你的實際代碼嗎?記得在通過jquery使用它們(即'$('。myClass')')時,在類名之前加上一個點'.'。 –

1

使用$('div').not(this)選擇比點擊一個其他。

0

使用:not().not()

$this; 

function myFunction(){ 
     // stuff here // and use $this for element reference 
} 


$('yourElement:not(.selected)').on('click',function(){ 
    // (make sure to add '.selected' to the new one and toggle existant) 
    $('.selected').removeClass('selected'); 
    $this = $(this); 
    $this.addClass('selected'); 

    myFunction(); 
}); 
0

這是錯誤的:

var newStatus = $(this).className().replace("contribution-", ""); 

className不是一個函數。

而是上面一行的你可以試試這個:

var cls = $(this).attr('class').replace('contribution-',''); 
$(this).removeClass().addClass(cls); 
相關問題