You can always simply configure delegation and then make your predicate inside the event handler:
ancestor.delegate('a[href^="#"]', 'click', function(ev) { if (someElement.find($(ev.target).attr('href')).length > 0) {
If you want to avoid price while doing this jQuery DOM search inside the handler, you can pre-tag all the โgoodโ tags:
$('a[href^="#"]').each(function() { if (someElement.find($(this).attr('href')).length > 0) $(this).addClass("special"); });
Then your delegated event handler can just check
if ($(ev.target).hasClass('special')) { // do stuff }
which will work well enough not to be a problem under any circumstances.
The reason you should start with the selector for ".delegate ()" is because it has implemented. An event handler always does something like:
function genericDelegateHandler(ev) { if ($(ev.target).is(theSelector)) { userHandler.call(this, ev); } }
Now, obviously, he can also try to compare the actual elements if you configured the delegate without a selector, but he just doesnโt.
edit - @DADU (OP) correctly indicates that if you encounter the problem of marking everything with the class name, then you donโt even need an event handler that tests; the usual ".delegate ()" will do this. :-)
source share