加载页面framents使用jQuery AJAX [英] loading page framents with Jquery AJAX
问题描述
我想用$阿贾克斯()请求一个页面,但加载该页面中的碎片。我知道你可以指定要使用.load()什么页面片段,但我不知道这是否可能与$阿贾克斯?
I would like to use $.ajax() to request a page, but load only fragments of that page. I know you can specify what page fragments you want with .load() but I was wondering if this is possible with $.ajax?
推荐答案
对于那些谁正在琢磨,stoplion是指该功能:的加载页面片段(在页面上向下滚动):
For those of you who are wondering, stoplion is referring to this feature: Loading Page Fragments (scroll down on the page):
该.load()方法,不像$。获得(),允许我们指定要插入远程文档的一部分。这是通过对URL参数的特殊语法来实现。 如果一个或多个空格字符包含字符串中,字符串的以下第一空间的部分被假定为一个jQuery选择来确定要加载的内容。
The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.
由于$。获得()似乎不支持的话,我认为$就不会任。实现一个简单的方法是如下:
Since $.get() doesn't appear to support it, I assume that $.ajax wouldn't either. A simple way to implement this would be the following:
$.ajax({
url: 'http://example.com/page.html',
data: {},
success: function (data) {
$("#el").html($(data).find("#selector"));
},
dataType: 'html'
});
这将是相当于
$("#el").load('http://example.com/page.html #selector');
不过,请注意,特殊语法('#selector)是指脚本present在加载HTML会的不可以执行。请参见脚本执行在.load()的文档。
However, note that the special syntax (' #selector') means that scripts present in the loaded HTML will not be executed. See Script Execution in the .load() docs.
这篇关于加载页面framents使用jQuery AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!