简体   繁体   中英

What is another JQuery function that does something similar to is()?

$target.is( "a.foo" ) will return true or false depending on if the target matches what's inside is().

What if I want to check what is() sees? What function do I use?

I tried alert($(target.is()) to see if I could find out what the target is, but I just got the value false.

you can try event.target :

The target property can be the element that registered for the event or a descendant of it. It is often useful to compare event.target to this in order to determine if the event is being handled due to event bubbling. This property is very useful in event delegation, when events bubble.

$('a').click(function(event){
     var $target = $(event.target);
     if( $target.is("a.foo") ) {
        ...
     }
})

我不确定我是否理解这个问题,但是根据您的主题,此处类似的jQuery函数将是.hasClass()

$target.hasClass('foo'); // true if $target contains a className 'foo'

u can use is() to check some variables to return a function; it is very similar to hasClass method..

$('a').hover(function() {
  $target = $(this);
  if(!$target.hasClass('active')) {//or u can use is('#active') to check id
                             //to prevent same response on same object hover
     $('a').removeClass('active');
     $target.addClass('active');
     $('.allPages').hide();
     var getID = $target.attr('id');
     $('#'+getID).show();

  };
});

Would this not be a matter if using console.log on the object, and on the typeof of both the $target and a selector of a.foo?

ie

console.log($('a.foo'));
console.log(typeof($('a.foo')));
console.log($target);
console.log(typeof($target));

I assume this is what the .is() would be checking, testing the value and type of two objects, similar to '===' although I may be wrong

i think this can help you

http://api.jquery.com/jQuery.type/

example is here :

jQuery.type(target);

For me it looks like you want to find out if the target was found. They way to do it:

if($target.length > 0){
    //found
}

If you want to get node name do:

$target.get(0).nodeName

All jQuery objects maintain their original selector in a property:

var $target = $('#originalSelector');

alert($target.selector); // Alerts '#originalSelector'

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