Home >Backend Development >PHP Tutorial >javascript - Ajax asynchronous loading, event triggering problem

javascript - Ajax asynchronous loading, event triggering problem

WBOY
WBOYOriginal
2016-12-01 01:27:471356browse

ajax asynchronously loads some pages (target-page). There is a button in the target-page. I want to set an onclick function for the button, but I find that it always cannot be triggered

Logic: Front-end jqueryload()(ajax asynchronous) loads the page, btn is placed in target-page

The pseudo code is as follows: (The code is rather verbose, so I replaced it with pseudo code)

ajax.js:

<code class="javascript">$(function(){
  $(xxx).load(target-page);
  function a(){codes}
  setTimeout(function(){
    $(btn).click(a);
    // 用奇葩方法settimeout解决
  }, 0);
});</code>

Can anyone help explain the principles and correct solutions??

Personally, I think the problem is that asynchronous loading is triggered after the basic js content (variable/function declaration) is loaded, which then leads to problems such as elements being unable to be obtained

Reply content:

ajax asynchronously loads some pages (target-page). There is a button in the target-page. I want to set an onclick function for the button, but I find that it always cannot be triggered

Logic: Front-end jqueryload()(ajax asynchronous) loads the page, btn is placed in target-page

The pseudo code is as follows: (The code is rather verbose, so I replaced it with pseudo code)

ajax.js:

<code class="javascript">$(function(){
  $(xxx).load(target-page);
  function a(){codes}
  setTimeout(function(){
    $(btn).click(a);
    // 用奇葩方法settimeout解决
  }, 0);
});</code>

Can anyone help explain the principles and correct solutions??

Personally, I think the problem is that asynchronous loading is triggered after the basic js content (variable/function declaration) is loaded, which then leads to problems such as elements being unable to be obtained

Let’s take a look at the documentation: http://api.jquery.com/load/

In addition, target-page is not a variable, what is it originally?

Reference code

<code class="javascript">// 第二个参数是 data,没有直接给 null,或者 {}
// 看文档中的例子,也可以省略掉,直接给 callback 作为第二个参数
$(xxx).load(url, function() {
    // do callback here
});</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn