Ajax asynchronously retrieves html data. The js method is invalid. ajaxjs

Source: Internet
Author: User

Ajax asynchronously retrieves html data. The js method is invalid. ajaxjs

The page uses js to write a method for obtaining background data.

Function data () {var tab = $ ("# dic") $. ajax ({url: '../demo. ashx? Method = getlist', data :{}, dataType: 'json', type: 'post', async: true, success: function (data) {// console. log (data); var parentStr = ''; $. each (data, function (I, item) {// console. log (item. text );

ParentStr + = "<div class = 'pull-Right'> <a class = 'morechang' href = 'javascript:; 'style = 'visibility: visible; '> More + </a> </div>"

});
Tab.html (parentStr );
}
})

}

The

<A class = 'moreid' href = 'javascript:; 'style = 'visibility: visible;'> More + </a> bind a click Time

$ ('. Morechange'). click (function () {alert ("pop-up ")});

Invalid click found

It turns out that JavaScript is loaded before ajax loads the new dom, and the event is not bound to the newly loaded dom, of course.

 

Solution:

Use the delegate event of jquery to delegate this method to an existing node on the page.

$ ("# Dic"). delegate ('. morechange', 'click', function () {alert ("pop-up ");});

Solve the problem.

Of course, it is also possible to change async to false without Using async asynchronously.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.