jquery load loading page inside Ajax returned Div does not respond to the problem of page JS solution

Source: Internet
Author: User

1. Preface

Because of the project needs, it is necessary to load a page and keep the div returned by Ajax responsive to the JS click event within its page. This is not the problem of solving the problem of page JS when loading pages to div with jquery load, because there is no such problem (jquery version: 1.10.2.js).

2. Essential reasons

Assume that the page has a $ (. btn1). Click (function () {}) event, you manually copy a div to the page that contains class BTN1, and then you will find that you clicked on that Div and will not be responding if you execute $ (. btn1) in the console It does include the newly added Div, the problem is that the current div is not bound to that event.

The question of title is essentially the problem described above.

3. Workaround

The previous Code

$ (document). Ready (function () {    $ (' [id^= ' Modifysettings_ "] '). Bind (" click ", Function () {        //some code    }}

The modified code

This function variable must be placed outside the ready method, otherwise it cannot be responded to var registerclickcheckbox = function () {$ (' [id^= ' Modifysettings_ "] '). Bind (" click ", function () {            //some code}$ (document). Ready (function () {    //new add    registerclickcheckbox (); $.ajax ({   type: "POST",   URL: "/myproject/settingl?mode=additem   Data: $ (" #addNewItem "). Serialize (),           DataType: "JSON",   success:function (data) {        $ (' #div0 '). After (' <div id= "Modifysettings_ ' +data.id+ '" value = "' +data.value+ '" >new item</div> ');            Rebind the click Operation             Registerclickcheckbox ();        },   error:function (data) {        alert ("Error:" + Data.responsetext);}}      );                      }                

4. Other (can skip, pure record)

If the Load page has two tab, and two tab common part of the code, the other page to load the page Two tab, the $.tab method use can be slightly flexible, do not need two tab to load the sub-page, just load one, Then another with JS to control to achieve the same function and expected.

function Hidealltabs () {for (var I=1; i<10;i++) {var evalstr = "$ (' role=\ ' tabpane ' +i+ ' \ '] '). Hide ()"; eval (evalstr);} $ (' [role= ' presentation "]). Removeclass (' active ');} function ShowTabs (tabsid) {hidealltabs (), if (tabsid! = ' Tabs_sub1 ') {$ ("#" +tabsid). Show (); $ ("A[href= ' #" +tabsid+ "']") . tab (' Show ');} else{$ (' #tabs_sub2 '). Show (); $ (' [href= ' #tabs_sub1 "] '). Parent (). addclass (' active ');}    } function Tabshandler (event) {    var data = Event.data;   ShowTabs (data.id);  TimeOut = SetTimeout (function () {$.ajax ({type: ' POST ', data: {mode: ' Savesettingtabindex ', Tabid:data.id},url: './ Siconfig ', Success:function (msg) {}});}, and return false; }$ (document). Ready (function () {$ (tabsdata). each (function () {        $ ("a[href= ' #" +this.id+ "']"). Bind (' click ', {ID: This.id},tabshandler);}); var tabId = ' <%= utils.getsettingtabindex ()%> '; for (var index in tabsdata) {if (tabsdata[index].id==tabid) showtabs (tabsdata[index].id);}}

JQuery load loading page within Ajax returned Div does not respond to the problem of page JS Solutions

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.