如何选择不是'this'的元素?

时间:2012-04-30 12:41:02

标签: javascript jquery

我有所有具有相同类的div列表,我想将一个函数应用于所有不是被点击的函数(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();
        });
    });
});

5 个答案:

答案 0 :(得分:7)

查看jQuerys not函数:http://api.jquery.com/not/

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

关于您的更新,请尝试:

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

答案 1 :(得分:3)

使用.not()功能:

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

答案 2 :(得分:1)

使用$('div').not(this)选择非点击的。

答案 3 :(得分: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();
});

答案 4 :(得分:0)

这是错误的:

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

className不是函数。

您可以尝试以下方式而不是上线:

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