I had to do this recently to an existing project, so I'd like to provide a conditional alternative to the selected answer... Depending on the complexity of the project in question, there is another option that could in my opinion be more efficient.
Quite simply:
Make it possible to target all applicable links:
Example: add class="no-link"
to all relevant <a>
elements.
Disable typical link functionality by targeting the appropriate CSS selector.
Example: apply the usual e.preventDefault()
override to elements matching $('a[href="#"].no-link')
JS Bin example.
That said, most often simpler is better.. If you don't need to add a custom class to numerous links on a page, then don't. :) Also be very careful when using this, having insufficiently specific jQuery selectors could result in some links being disabled when you don't expect them to be.