javascript - Ajax asynchronous loading, event triggering problem

WBOY
Release: 2023-03-02 13:18:01
Original
1304 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!