开发者

How to create an if statement for jQuery, click event

I do have a css class name ".className" How to create an if statement wherein if the user does not click .className it will alert. for example:

if (click != $('.className')) {
   alert("You did not click className");
}

I know that my code is incorrect. How to do it? Btw, this is similar to an outside click. Th开发者_开发百科ank you.


$("a").click(function(e) {
    e.preventDefault();
    if (!$(this).hasClass('className')) {
        alert("You did not click className!");
    }
});

demo: http://jsfiddle.net/yaqLs/


EDIT: If by "outside clicks" you mean not just links then,

$("*").click(function(e) {
    e.stopPropagation();
    if (!$(this).hasClass('className')) {
        alert("You did not click className!");
    }
});

demo: http://jsfiddle.net/yaqLs/1/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜