簡體   English   中英

如何選擇不是'this'的元素?

[英]How to select element which is not 'this'?

我有所有具有相同類的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();
        });
    });
});

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

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

關於您的更新,請嘗試:

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

使用.not()函數:

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

使用$('div').not(this)選擇非點擊的。

使用: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();
});

這是錯的:

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

className不是函數。

而不是上面的線你可以試試這個:

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