开发者

jquery bubbling up the dom to see if there is any match to an ID

I have a help div that becomes visible, I made it so that if a click is outside the div, it closes the help div.

I want the div to remain open if the user click inside the div. For regular Javasc开发者_如何转开发ript, I use the bubbling up code to match the parent's id. My partial JS code is

var click = (e && e.target) || (event && event.srcElement);
var match = "divid";

while(click.parentNode){
        if(click==match) {
        return;//do nothing
            }//if

what is the jquery equivalent to this recursive javascript code?

TIA

edited to avoid confusion.


You could return false and it'll work. Or you could use stopPropagation() which is a utility function built into jQuery:

jQuery("#divid").click(function(event) {
  event.stopPropagation();
});

Fiddle here: http://jsfiddle.net/W8MfD/

The return false method will still bubble up before returning false, as I understand it.

I do tend to believe that if you anticipate using lots of these kinds of events, you would benefit from using some sort of widget framework. The kind of behaviour you're creating is commonplace and most frameworks already have it. jQuery UI isn't bad (though in my opinion it could be better). There are others out there, too.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜