<div class="SubMenu"><ul><li><a href="#">One</a></li><li><a href="#">Two</a></li><li><a href="#">Three</a></li></ul></div>
Now I wanted to fire a function when i click on div which has class "SubMenu" but the problem is i wanted to see if i have clicked there, i do not want to do like $(.'SubMenu').hide() instead i wanted to check that where i 开发者_如何学编程have clicked has a class SubMenu or not.
Issue here is i am not getting the element arrt dont know whats wrong, please help
You can use hasClass
to check if an element has a particular class.
$('div').click( function() {
alert( $(this).hasClass('SubMenu') );
});
// outputs true or false
I am not sure what you are trying to achieve. I assume that you are looking to hide a menu on document click. If you are after this you can do something like this
$(function(){
$(document).click(function(){
if(!$(this).hasClass('submenu'))
{
$('div.submenu').hide();
}
});
$("div.submenu").click(function(e){
e.stopPropagation(); // stops calling document click
});
});
精彩评论