简体   繁体   中英

How to select element which is not 'this'?

I have list of divs all with the same class, I want to apply a function to all of them which are not the clicked one ( this ), how can i select !this with jQuery?

UPDATE: I've made this and it is not working, any ideas why?

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

UPDATE 2: this is the whole actual code:

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

Have a look at jQuerys not function: http://api.jquery.com/not/

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

Regarding your update, try:

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

Use the .not() function:

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

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

using :not() or .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();
});

this is wrong:

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

className is not a function.

Instead of above line you can try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM