How would you write the Jquery to get the closest div that actually has an ID defined?
gor
You should use has attribute selector. This sample should do the work:
$('selector').closest('[id]')
$(elementToStart).parent().closest('div[id]');
I use the parent() to avoid just getting the element itself.
Example: http://jsfiddle.net/zQRFT/1/
Look for an id attribute on a div, using the closest method:
$(this).closest('div[id]');
The [id]
brackets there is what's called the Has Attribute Selector
来源:https://stackoverflow.com/questions/5096103/jquery-closest-div-that-has-an-id