The click event becomes invalid after ajax requests data in javascript-js.

Source: Internet
Author: User
I have a data list with a click to expand it. Because ajax loading is used for this data list, the click time after ajax loading does not work. I hope you can help us to see it. This is the expanded click event and aj... in the data structure and page layout {code...} js in html. This data list uses ajaxLoad is ajaxAfter loading clickTime does not work anymore. I hope you can take a look.
This is htmlData structure and page layout in

Consumer information will gradually increase. if a manufacturer uses better raw materials and technologies, it will quickly occupy the market.

Time: | Source: expand on this site

Consumer information will gradually increase. if a manufacturer uses better raw materials and technologies, it will quickly occupy the market.

Time: | Source: expand on this site

Consumer information will gradually increase. if a manufacturer uses better raw materials and technologies, it will quickly occupy the market.

Time: | Source: expand on this site

Consumer information will gradually increase. if a manufacturer uses better raw materials and technologies, it will quickly occupy the market.

Time: | Source: expand on this site

jsExpandclickEvents andajaxThe request is as follows:

Script; (function ($) {// open $ ('. open-detail '). click (function () {$ (this ). parent (). siblings ('P [class = "no-open"] '). addClass ('yes-open'); $ (this ). text ('collapse '); $ (this ). attr ('class', 'close-detail') ;}); // collapse $ ('. close-detail '). click (function () {$ (this ). parent (). siblings ('P [class = "yes-open"] '). addClass ('no-open'); $ (this ). text ('expand '); $ (this ). attr ('class', 'Open-detail') ;}); //}); function getAjaxList (_ v, _ C, _ t) {$. ajax ({url: './ajax. php? Op = '+ _ v, type: 'post', dataType: 'json', data: {'Category': _ c, 'template': _ t}, success: function (msg) {if (msg. status = 1) {certificate ('.html-hide').html(msg.html); certificate ('.html-hide '). siblings ('P '). hide (); require ('.html-hide '). show () ;}else {detail ('.list-data'detail .html ('Here you do not want any information, go to the latest news to see ') ;}}} script

phpInpSettings and data settings


  $ Value) {$ html. ='

'; $ Html. ='

'. $ Value ['desc'].'

'; $ Html. ='

Time: '. date ('Y-m-D', $ value ['updatetime']).' | Source: '. $ value ['author'].' expand

'; $ Html. ='

';}$ Result ['html'] = $ html; exit (json_encode ($ result ));

OnphpMediumpAll styles in! But it cannot.

Reply content:

I have a data list with a click to expand it. This data list usesajaxLoad isajaxAfter loadingclickTime does not work anymore. I hope you can take a look.
This ishtmlData structure and page layout in

Consumer information will gradually increase. if a manufacturer uses better raw materials and technologies, it will quickly occupy the market.

Time: | Source: expand on this site

Consumer information will gradually increase. if a manufacturer uses better raw materials and technologies, it will quickly occupy the market.

Time: | Source: expand on this site

Consumer information will gradually increase. if a manufacturer uses better raw materials and technologies, it will quickly occupy the market.

Time: | Source: expand on this site

Consumer information will gradually increase. if a manufacturer uses better raw materials and technologies, it will quickly occupy the market.

Time: | Source: expand on this site

jsExpandclickEvents andajaxThe request is as follows:

Script; (function ($) {// open $ ('. open-detail '). click (function () {$ (this ). parent (). siblings ('P [class = "no-open"] '). addClass ('yes-open'); $ (this ). text ('collapse '); $ (this ). attr ('class', 'close-detail') ;}); // collapse $ ('. close-detail '). click (function () {$ (this ). parent (). siblings ('P [class = "yes-open"] '). addClass ('no-open'); $ (this ). text ('expand '); $ (this ). attr ('class', 'Open-detail') ;}); //}); function getAjaxList (_ v, _ C, _ t) {$. ajax ({url: './ajax. php? Op = '+ _ v, type: 'post', dataType: 'json', data: {'Category': _ c, 'template': _ t}, success: function (msg) {if (msg. status = 1) {certificate ('.html-hide').html(msg.html); certificate ('.html-hide '). siblings ('P '). hide (); require ('.html-hide '). show () ;}else {detail ('.list-data'detail .html ('Here you do not want any information, go to the latest news to see ') ;}}} script

phpInpSettings and data settings


  $ Value) {$ html. ='

'; $ Html. ='

'. $ Value ['desc'].'

'; $ Html. ='

Time: '. date ('Y-m-D', $ value ['updatetime']).' | Source: '. $ value ['author'].' expand

'; $ Html. ='

';}$ Result ['html'] = $ html; exit (json_encode ($ result ));

OnphpMediumpAll styles in! But it cannot.

Thank you for your invitation. I have encountered this kind of problem before.ajax()The added data fails. The following is my processing method:

// This is what I wrote during data initialization $ ('. message-'). click (function () {// code ...}); // after ajax is loaded, the click event is not executed and changed to $ ('. message-'). live ("click", function () {// code ...});

Then you can find this information through online query.live()Actually andbind()Similar to the trigger event, the trigger event can be bound to future elements, but the two are different. Try both of them. I hope they will help you.

There is no live method after jquery1.9 +.

For versions earlier than jQuery1.9, we can write as follows:

$("a").live("focus",function(){  this.blur();}); 

After jQuery1.9, because live is deleted, it should be written as follows:

$(document).on("focus","a",function(){  this.blur();}); 

I have roughly looked at it.list-data-detailAll are dynamically loaded using Ajax, so you can directly writeclickEvents do not work, because the DOM does not exist when your events are bound.
At this time, you need to useEvent delegate, InjQuery, That is

$('.list-data').on('click', '.open-detail', function() {    /// here goes the code});

That's all. Through event delegation, events of child elements are bound to the parent element. after a child element is clicked, the event bubbles to the parent element, which is responsible for capturing and triggering the event.

Click the dynamically loaded element.

$(document).on('click','.message-but',function(){    // code...});

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.