5

すべて同じクラスのdivのリストがありますが、クリックされたもの()ではないすべてのdivに関数を適用したいのですが、jQuerythisでどのように選択できますか?!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();
        });
    });
});
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

これは間違っています:

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

className関数ではありません。

上記の行の代わりに、これを試すことができます:

var cls = $(this).attr('class').replace('contribution-','');
$(this).removeClass().addClass(cls);
于 2012-04-30T13:22:48.067 に答える
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 に答える