Monday, 20 March 2017

javascript - live() alternative on() not working jQuery 1.9



I have code do ajax method for loading new content
But I have problem with the new content, the links not applying the action i did like
preventDefault and the ajax method, just after loading the new content
clicking on links make the page reload like there was no ajax code at all
In JQ 1.8 working grate with live() method, but after updating jQuery, not working as it should with on() method




The old code working right and have no problem with it







The new updated code







The problem right here




$('.nav-menu li a,#nav-below a,#main a').on('click',function(e) {


Thank you :)


Answer



You don't simply s/live/on, you need to read the documentation of on() to see the changes required to update your code (on is similar to the old delegate()).



If you want to make it work exactly like live(), try...



$(document).on('click', '.nav-menu li a,#nav-below a,#main a', function(e) { });



However, if you can, you're better off choosing the most common persistent ancestor. This means that the events won't have to go all the way up to document to be handled. For example, body probably covers 99.9% of situations. However, it's probably more like #some-container.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...