简体   繁体   中英

jQuery Mobile Select all a Tags in all Pages

How can you select all <a> tags in jQuery Mobile if you have more pages?

I tried it with jQuery("a")

Depending on what version of jQuery [Mobile] you use you should be able to bind an event to items that don't (yet) exist on the page with either the live or on and the on method is preferred.

The live method will bind an event to the document and execute it when the event is triggered on an element matching the given selector:

$("a").live('click', function () { /* do something on click with the 'a' element clicked */ });

The on method is better since it doesn't depend the even bubbling up to the document before executing.

Instead you put the event on a parent object of the elements that will be created in the future but already exists and you supply a selector to specify which elements within that selector 'qualify' to get the event called on. In your case to select all 'a' tags on the page, doing the same as the above code:

$("body").on('click', 'a', function () { /* do something on click with the 'a' element clicked */ }); 

If you want to do something with the a tags as soon as they are loaded through ajax you should do this in the complete callback for your ajax request loading them. This can possibly be done by appending the .done(callback) to your $.get() or $.ajax() request.

Please read the documentation for a good explanation on .done()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM