开发者

Dont hide if it hasClass in jQuery

开发者 https://www.devze.com 2023-04-08 02:44 出处:网络
Okay, start out saying this is hard to explain with words only. So i have setup a example here: http://jsfiddle.net/AFhvS/2/

Okay, start out saying this is hard to explain with words only.

So i have setup a example here: http://jsfiddle.net/AFhvS/2/

I have a code in PHP, that show() the span element with X .sletindtastningfelt if it's filled.

In the jsfiddle, it's placed at the bottom in the js field.

jQuery(document).ready(function() {
    $('#smeal-1-1').show();
    $('#smeal-1-1').addClass("upstartRemoveClass");

    $('#smeal-1-2').show();
    $('#smeal-1-2').addClass("upstartRemoveClass");
}); 

Ok so it appears X infront of the first two input fields now.

What it does now is that when you click on one of the 4 tr (input fields), it hides the other X's and keeps the one in开发者_开发知识库front of the field. This is caused by the .end().siblings().find('.sletindtastningfelt').hide();

I wish to change this procedure if it has the class "upstartRemoveClass". So if it has the class upstartRemoveClass it should not hide, like the rest, when you click on a tr (input field)


Maybe you can use :not(.className):

.end().siblings().find('.sletindtastningfelt:not(.upstartRemoveClass)').hide();


How about using not?

Like this:

.end().siblings().find('.sletindtastningfelt').not(".upstartRemoveClass").hide();

Does that give you the result you're looking for?


This just requires a condition:

if($("someElement").hasClass("upstartRemoveClass"))
{
   // do something
}


The answers above using .not() or :not() are valid, but you can also change the logic a bit, and show any .upstartRemoveClass after hiding the rest:

$(this).closest('tr')
       .find('.sletindtastningfelt')
       .show()
       .end()
       .siblings()
       .find('.sletindtastningfelt')
       .hide();

$('.upstartRemoveClass').show();

In this case, it will probably not be the best choice, but it can be useful sometimes, for example to remove complexity.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号