5

我有所有具有相同类的 div 列表,我想将一个函数应用于所有不是单击的 ( this) 的函数,我如何!this使用 jQuery 进行选择?

更新:我做了这个,但它不起作用,有什么想法吗?

    $("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();
        });
    });
});
4

5 回答 5

7

看看 jQuerysnot函数:http ://api.jquery.com/not/

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

关于您的更新,请尝试:

        $("li").click(function(e) {
            $("li").not(this).hide();
        });
于 2012-04-30T12:42:55.057 回答
5

使用.not()功能:

$('.yourclass').click(function() {
    $('.yourclass').not(this).yourFunc();
});
于 2012-04-30T12:43:04.737 回答
1

用于$('div').not(this)选择单击以外的其他选项。

于 2012-04-30T12:43:59.387 回答
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();
});
于 2012-04-30T12:45:49.037 回答
0

这是错误的:

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

className不是函数。

而不是上面的行,你可以试试这个:

var cls = $(this).attr('class').replace('contribution-','');
$(this).removeClass().addClass(cls);
于 2012-04-30T13:22:48.067 回答