简体   繁体   English

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

[英]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? 我有所有具有相同类的div列表,我想将一个函数应用于所有这些不是被点击的( this ),我如何选择!this与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: 更新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();
        });
    });
});

Have a look at jQuerys not function: http://api.jquery.com/not/ 看看jQuerys not功能: 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: 使用.not()函数:

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

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

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

this is wrong: 这是错的:

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

className is not a function. className不是函数。

Instead of above line you can try this: 而不是上面的线你可以试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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