Home  >  Article  >  Web Front-end  >  All ajax is executed and the page is loaded.

All ajax is executed and the page is loaded.

巴扎黑
巴扎黑Original
2017-07-03 11:03:333750browse

jquery ajax&load method causes the js effect not to be displayed or the effect error occurs due to ajax relayout of the page after loading.

Solution: You need to execute the method after ajax get post or load is completed to avoid final errors caused by their incomplete execution.

Then first look at the load method definition:

jQuery ajax - load() 方法

jQuery Ajax 参考手册
实例

使用 AJAX 请求来改变 p 元素的文本:

$("button").click(function(){
  $("p").load('demo_ajax_load.txt');
});

亲自试一试

您可以在页面底部找到更多 TIY 实例
定义和用法

load() 方法通过 AJAX 请求从服务器加载数据,并把返回的数据放置到指定的元素中。

注释:还存在一个名为 load 的 jQuery 事件方法。调用哪个,取决于参数。
语法

load(url,data,function(response,status,xhr))

参数 	描述
url 	规定要将请求发送到哪个 URL。
data 	可选。规定连同请求发送到服务器的数据。
function(response,status,xhr) 	

可选。规定当请求完成时运行的函数。

额外的参数:

    response - 包含来自请求的结果数据
    status - 包含请求的状态("success", "notmodified", "error", "timeout" 或 "parsererror")
    xhr - 包含 XMLHttpRequest 对象

详细说明

该方法是最简单的从服务器获取数据的方法。它几乎与 $.get(url, data, success) 等价,不同的是它不是全局函数,并且它拥有隐式的回调函数。当侦测到成功的响应时(比如,当 textStatus 为 "success" 或 "notmodified" 时),.load() 将匹配元素的 HTML 内容设置为返回的数据。这意味着该方法的大多数使用会非常简单:

$("#result").load("ajax/test.html");

如果提供回调函数,则会在执行 post-processing 之后执行该函数:

$("#result").load("ajax/test.html", function() {
  alert("Load was performed.");
});

上面的两个例子中,如果当前文档不包含 "result" ID,则不会执行 .load() 方法。

如果提供的数据是对象,则使用 POST 方法;否则使用 GET 方法。
加载页面片段

.load() 方法,与 $.get() 不同,允许我们规定要插入的远程文档的某个部分。这一点是通过 url 参数的特殊语法实现的。如果该字符串中包含一个或多个空格,紧接第一个空格的字符串则是决定所加载内容的 jQuery 选择器。

我们可以修改上面的例子,这样就可以使用所获得文档的某部分:

$("#result").load("ajax/test.html #container");

如果执行该方法,则会取回 ajax/test.html 的内容,不过然后,jQuery 会解析被返回的文档,来查找带有容器 ID 的元素。该元素,连同其内容,会被插入带有结果 ID 的元素中,所取回文档的其余部分会被丢弃。

jQuery 使用浏览器的 .innerHTML 属性来解析被取回的文档,并把它插入当前文档。在此过程中,浏览器常会从文档中过滤掉元素,比如 <html>, <title> 或 <head> 元素。结果是,由 .load() 取回的元素可能与由浏览器直接取回的文档不完全相同。

注释:由于浏览器安全方面的限制,大多数 "Ajax" 请求遵守同源策略;请求无法从不同的域、子域或协议成功地取回数据。
更多实例
例子 1

加载 feeds.html 文件内容:

$("#feeds").load("feeds.html");

例子 2

与上面的实例类似,但是以 POST 形式发送附加参数并在成功时显示信息:

$("#feeds").load("feeds.php", {limit: 25}, function(){
  alert("The last 25 entries in the feed have been loaded");
});

例子 3

加载文章侧边栏导航部分至一个无序列表:

HTML 代码:

<b>jQuery Links:</b>
<ul id="links"></ul>

jQuery 代码:

$("#links").load("/Main_Page #p-Getting-Started li");


I found that there is a callback method, that’s great. If I write my own method in the callback, it will be executed after ajax. Ever since:

$("#feeds").load("feeds.php", function(){
    fun();
});

But there is a problem, what if there are more than one? Add after each one? It also determines whether this method has been executed. (...)

So: Find a jquery built-in judgment and then execute it after all is completed, then:

	$("p").ajaxStop(function(){
		if (hash && !isGlobalHash) {
			$("#MfTit"+hash).trigger("click");
			isGlobalHash = true;
		}
	});

Definition:

Example

When all AJAX requests are completed, a prompt box is triggered:

$("p").ajaxStop(function(){
  alert("所有 AJAX 请求已完成");
});

Of course, if there is a stop, there will be a start.

The above is the detailed content of All ajax is executed and the page is loaded.. For more information, please follow other related articles on the PHP Chinese website!

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