responseXML无法取得结果

yixianjushi 2012-06-13 05:39:22
代码如下:
<?php
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");

$q = $_GET["q"];


// include_once("../public/dbManager.php");
// include_once("../public/stringConvert.php");;


$con = mysql_connect('localhost', 'root', 'root');
if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("ajax_demo", $con);

$sql = "SELECT * FROM user WHERE id = " . $q . "";

$result = mysql_query($sql);

echo '<?xml version="1.0" encoding="ISO-8859-1"?>
<person>';
while ($row = mysql_fetch_array($result)) {
echo "<firstname>" . $row['FirstName'] . "</firstname>";
echo "<lastname>" . $row['LastName'] . "</lastname>";
echo "<age>" . $row['Age'] . "</age>";
echo "<hometown>" . $row['Hometown'] . "</hometown>";
echo "<job>" . $row['Job'] . "</job>";
}
echo "</person>";

mysql_close($con);
?>

上述代码只要引入了其他文件,注释部分
// include_once("../public/dbManager.php");
// include_once("../public/stringConvert.php");
所示。
responseXML 便无法获取结果,并且执行到xmlDoc.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
处程序便不再执行。
如果去掉include语句,程序运行正常。
上述引入的文件没有任何页面输出。
本人刚接触php ,请高手解答此为何故, 谢谢。
...全文
219 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yixianjushi 2012-06-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

那还能为什么,只能因为包含了不合法字符了,还是要仔细检查检查。
[/Quote]

这个说法感觉靠谱,确实想不出其他的原因。
yixianjushi 2012-06-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
将这两句注释掉。将包含文件打开。打开错误提示功能,看是否报错。
[/Quote]

你说的试过了,没有任何错误提示。
一起混吧 2012-06-13
  • 打赏
  • 举报
回复
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");

将这两句注释掉。将包含文件打开。打开错误提示功能,看是否报错。
qq120848369 2012-06-13
  • 打赏
  • 举报
回复
那还能为什么,只能因为包含了不合法字符了,还是要仔细检查检查。
yixianjushi 2012-06-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

先看看XML是否正确返回的再说,调试功夫不能没有吧。
[/Quote]

xml 返回当然是正确的了,直接处理responseText也能解决问题,只是不明白为何引入了其他文件,responseXML访问就会失败
  • 打赏
  • 举报
回复
PHP数组与xml相互转换封装函数:xmlparse.php

#将数组转为xml
include(dirname(__FILE__)."/xmlparse.php");
$listtags = explode(" ","name");
$arr = array(1,"name"=>"SOM","admin",100,array("name"=>"TOM","Lily"));
$contXML = dump_xml_config($arr, "xml", "utf-8");
//放入文件
//file_put_contents_safe("/path/test.xml",$contXML,"w");
echo $contXML;
#源码
#<?xml version="1.0" encoding="utf-8"?>
#<xml>
# <0>1</0>
# <name>SOM</name>
# <1>admin</1>
# <2>100</2>
# <3>
# <name>TOM</name>
# <0>Lily</0>
# </3>
#</xml>
qq120848369 2012-06-13
  • 打赏
  • 举报
回复
先看看XML是否正确返回的再说,调试功夫不能没有吧。
jQuery1.2 API 中文版折叠展开折叠全部展开全部 英文说明 核心jQuery 核心函数 jQuery(expression,[context]) jQuery(expression,[context]) 这个函数接收一个包含 CSS 选择器的字符串,然后用这个字符串去匹配一组元素。 jQuery 的核心功能都是通过这个函数实现的。 jQuery中的一切都构建于这个函数之上,或者说都是在以某种方式使用这个函数。这个函数最基本的用法就是向它传递一个表达式(通常由 CSS 选择器组成),然后根据这个表达式来查找所有匹配的元素。 默认情况下, 如果没有指定context参数,$()将在当前的 HTML 文档中查找 DOM 元素;如果指定了 context 参数,如一个 DOM 元素集或 jQuery 对象,那就会在这个 context 中查找。 参考 Selectors 获取更多用于 expression 参数的 CSS 语法的信息。 -------------------------------------------------------------------------------- This function accepts a string containing a CSS selector which is then used to match a set of elements. The core functionality of jQuery centers around this function. Everything in jQuery is based upon this, or uses this in some way. The most basic use of this function is to pass in an expression (usually consisting of CSS), which then finds all matching elements. By default, if no context is specified, $() looks for DOM elements within the context of the current HTML document. If you do specify a context, such as a DOM element or jQuery object, the expression will be matched against the contents of that context. See Selectors for the allowed CSS syntax for expressions. 返回值 jQuery 参数 expression (String) : 用来查找的字符串 context (Element, jQuery) : (可选) 作为待查找的 DOM 元素集、文档或 jQuery 对象。 示例 找到所有 p 元素,并且这些元素都必须是 div 元素的子元素。 HTML 代码:

one

two

three

jQuery 代码: $("div > p"); 结果: [

two

] -------------------------------------------------------------------------------- 在文档的第一个表单中,查找所有的单选按钮(即: type 值为 radio 的 input 元素)。 jQuery 代码: $("input:radio", document.forms[0]); -------------------------------------------------------------------------------- 在一个由 AJAX 返回的 XML 文档中,查找所有的 div 元素。 jQuery 代码: $("div", xml.responseXML); jQuery(html)jQuery(html) 根据提供的原始 HTML 标记字符串,动态创建由 jQuery 对象包装的 DOM 元素。 你可以传递一个手写的 HTML 字符串,或者由某些模板引擎或插件创建的字符串,也可以是通过 AJAX 加载过来的字符串。但是在你创建 input 元素的时会有限制,可以参考第二个示例。当然这个字符串可以包含斜杠 (比如一个图像地址),还有反斜杠。当你创建单个元素时,请使用闭合标签或 XHTML 格式。例如,创建一个 span ,可以用 $("") 或 $("") ,但不推荐 $("") -------------------------------------------------------------------------------- Create DOM elements on-the-fly from the provided String of raw HTML. You can pass in plain HTML Strings written by hand, create them using some template engine or plugin, or load them via AJAX. There are limitations when creating input elements, see the second example. Also when passing strings that may include slashes (such as an image path), escape the slashes. When creating single elements use the closing tag or XHTML format. For example, to create a span use $("") or $("") instead of without the closing slash/tag. 返回值 jQuery 参数 html (String) : 用于动态创建DOM元素的HTML标记字符串 示例 动态创建一个 div 元素(以及其中的所有内容),并将它追加到 body 元素中。在这个函数的内部,是通过临时创建一个元素,并将这个元素的 innerHTML 属性设置为给定的标记字符串,来实现标记到 DOM 元素转换的。所以,这个函数既有灵活性,也有局限性。 jQuery 代码: $("

Hello

").appendTo("body"); -------------------------------------------------------------------------------- 创建一个 元素必须同时设定 type 属性。因为微软规定 元素的 type 只能写一次。 jQuery 代码: // 在 IE 中无效: $("").attr("type", "checkbox"); // 在 IE 中有效: $(""); jQuery(elements)jQuery(elements) 将一个或多个DOM元素转化为jQuery对象。 这个函数也可以接收XML文档和Window对象(虽然它们不是DOM元素)作为有效的参数。 -------------------------------------------------------------------------------- Wrap jQuery functionality around a single or multiple DOM Element(s). This function also accepts XML Documents and Window objects as valid arguments (even though they are not DOM Elements). 返回值 jQuery 参数 elements (Element, Array) : 用于封装成jQuery对象的DOM元素 示例 设置页面背景色。 jQuery 代码: $(document.body).css( "background", "black" ); -------------------------------------------------------------------------------- 隐藏一个表单中所有元素。 jQuery 代码: $(myForm.elements).hide() jQuery(callback)jQuery(callback) $(document).ready()的简写。 允许你绑定一个在DOM文档载入完成后执行的函数。这个函数的作用如同$(document).ready()一样,只不过用这个函数时,需要把页面中所有需要在 DOM 加载完成时执行的$()操作符都包装到其中来。从技术上来说,这个函数是可链接的--但真正以这种方式链接的情况并不多。 你可以在一个页面中使用任意多个$(document).ready事件。 参考 ready(Function) 获取更多 ready 事件的信息。 -------------------------------------------------------------------------------- A shorthand for $(document).ready(). Allows you to bind a function to be executed when the DOM document has finished loading. This function behaves just like $(document).ready(), in that it should be used to wrap other $() operations on your page that depend on the DOM being ready to be operated on. While this function is, technically, chainable - there really isn't much use for chaining against it. You can have as many $(document).ready events on your page as you like. See ready(Function) for details about the ready event. 返回值 jQuery 参数 callback (Function) : 当DOM加载完成后要执行的函数 示例 当DOM加载完成后,执行其中的函数。 jQuery 代码: $(function(){ // Document is ready }); -------------------------------------------------------------------------------- Uses both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias. jQuery 代码: jQuery(function($) { // Your code using failsafe $ alias here... }); jQuery 对象访问 each(callback)each(callback) 以每一个匹配的元素作为上下文来执行一个函数。 意味着,每次执行传递进来的函数时,函数中的this关键字都指向一个不同的DOM元素(每次都是一个不同的匹配元素)。 而且,在每次执行函数时,都会给函数传递一个表示作为执行环境的元素在匹配的元素集合中所处位置的数字值作为参数(从零开始的整形)。 返回 'false' 将停止循环 (就像在普通的循环中使用 'break')。返回 'true' 跳至下一个循环(就像在普通的循环中使用'continue')。 -------------------------------------------------------------------------------- Execute a function within the context of every matched element. This means that every time the passed-in function is executed (which is once for every element matched) the 'this' keyword points to the specific DOM element. Additionally, the function, when executed, is passed a single argument representing the position of the element in the matched set (integer, zero-index). Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop). 返回值 jQuery 参数 callback (Function) : 对于每个匹配的元素所要执行的函数 示例 迭代两个图像,并设置它们的 src 属性。注意:此处 this 指代的是 DOM 对象而非 jQuery 对象。 HTML 代码: jQuery 代码: $("img").each(function(i){ this.src = "test" + i + ".jpg"; }); 结果: [ , ] -------------------------------------------------------------------------------- 如果你想得到 jQuery对象,可以使用 $(this) 函数。 jQuery 代码: $("img").each(function(){ $(this).toggleClass("example"); }); -------------------------------------------------------------------------------- 你可以使用 'return' 来提前跳出 each() 循环。 HTML 代码:
Stop here
jQuery 代码: $("button").click(function () { $("div").each(function (index, domEle) { // domEle == this $(domEle).css("backgroundColor", "yellow"); if ($(this).is("#stop")) { $("span").text("Stopped at div index #" + index); return false; } }); });size()size() jQuery 对象中元素的个数。 这个函数的返回值与 jQuery 对象的'length' 属性一致。 -------------------------------------------------------------------------------- The number of elements in the jQuery object. This returns the same number as the 'length' property of the jQuery object. 返回值 Number 示例 计算文档中所有图片数量 HTML 代码: jQuery 代码: $("img").size(); 结果: 2 lengthlength jQuery 对象中元素的个数。 当前匹配的元素个数。 size 将返回相同的值。 -------------------------------------------------------------------------------- The number of elements in the jQuery object. The number of elements currently matched. The size function will return the same value. 返回值 Number 示例 计算文档中所有图片数量 HTML 代码: jQuery 代码: $("img").length; 结果: 2 get()get() 取得所有匹配的 DOM 元素集合。 这是取得所有匹配元素的一种向后兼容的方式(不同于jQuery对象,而实际上是元素数组)。 如果你想要直接操作 DOM 对象而不是 jQuery 对象,这个函数非常有用。 -------------------------------------------------------------------------------- Access all matched DOM elements. This serves as a backwards-compatible way of accessing all matched elements (other than the jQuery object itself, which is, in fact, an array of elements). It is useful if you need to operate on the DOM elements themselves instead of using built-in jQuery functions. 返回值 Array 示例 选择文档中所有图像作为元素数组,并用数组内建的 reverse 方法将数组反向。 HTML 代码: jQuery 代码: $("img").get().reverse(); 结果: [ ] get(index)get(index) 取得其中一个匹配的元素。 num表示取得第几个匹配的元素。 这能够让你选择一个实际的DOM 元素并且对他直接操作,而不是通过 jQuery 函数。$(this).get(0)与$(this)[0]等价。 -------------------------------------------------------------------------------- Access a single matched DOM element at a specified index in the matched set. This allows you to extract the actual DOM element and operate on it directly without necessarily using jQuery functionality on it. This function called as $(this).get(0) is the equivalent of using square bracket notation on the jQuery object itself like $(this)[0]. 返回值 Element 参数 index (Number) :取得第 index 个位置上的元素 示例 HTML 代码: jQuery 代码: $("img").get(0); 结果: [ ] index(subject)index(subject) 搜索与参数表示的对象匹配的元素,并返回相应元素的索引值值。 如果找到了匹配的元素,从0开始返回;如果没有找到匹配的元素,返回-1。 -------------------------------------------------------------------------------- Searches every matched element for the object and returns the index of the element, if found, starting with zero. Returns -1 if the object wasn't found. 返回值 Number 参数 subject (Element) : 要搜索的对象 示例 返回ID值为foobar的元素的索引值值。 HTML 代码:
jQuery 代码: $("*").index($('#foobar')[0]) 结果: 5 插件机制 jQuery.fn.extend(object)jQuery.fn.extend(object) 扩展 jQuery 元素集来提供新的方法(通常用来制作插件)。 查看这里Plugins/Authoring可以获取更多信息。 -------------------------------------------------------------------------------- Extends the jQuery element set to provide new methods (used to make a typical jQuery plugin). Can be used to add functions into the to add plugin methods (plugins). 返回值 jQuery 参数 object (Object) :用来扩充 jQuery 对象。 示例 增加两个插件方法。 jQuery 代码: jQuery.fn.extend({ check: function() { return this.each(function() { this.checked = true; }); }, uncheck: function() { return this.each(function() { this.checked = false; }); } }); 结果: $("input[@type=checkbox]").check(); $("input[@type=radio]").uncheck(); jQuery.extend(object)jQuery.extend(object) 扩展jQuery对象本身。 用来在jQuery命名空间上增加新函数。 查看 'jQuery.fn.extend' 获取更多添加插件的信息。 -------------------------------------------------------------------------------- Extends the jQuery object itself. Can be used to add functions into the jQuery namespace. See 'jQuery.fn.extend' for more information on using this method to add Plugins. 返回值 jQuery 参数 object (Object) : 用以扩展 jQuery 对象 示例 在jQuery命名空间上增加两个函数。 jQuery 代码: jQuery.extend({ min: function(a, b) { return a < b ? a : b; }, max: function(a, b) { return a > b ? a : b; } }); 结果: jQuery.min(2,3); // => 2 jQuery.max(4,5); // => 5 多库共存 jQuery.noConflict()jQuery.noConflict() 运行这个函数将变量$的控制权让渡给第一个实现它的那个库。 这有助于确保jQuery不会与其他库的$对象发生冲突。在运行这个函数后,就只能使用jQuery变量访问jQuery对象。例如,在要用到$("div p")的地方,就必须换成jQuery("div p")。 -------------------------------------------------------------------------------- Run this function to give control of the $ variable back to whichever library first implemented it. This helps to make sure that jQuery doesn't conflict with the $ object of other libraries. By using this function, you will only be able to access jQuery using the 'jQuery' variable. For example, where you used to do $("div p"), you now must do jQuery("div p"). 返回值 jQuery 示例 将$引用的对象映射回原始的对象。 jQuery 代码: jQuery.noConflict(); // 使用 jQuery jQuery("div p").hide(); // 使用其他库的 $() $("content").style.display = 'none'; -------------------------------------------------------------------------------- 恢复使用别名$,然后创建并执行一个函数,在这个函数的作用域中仍然将$作为jQuery的别名来使用。在这个函数中,原来的$对象是无效的。这个函数对于大多数不依赖于其他库的插件都十分有效。 jQuery 代码: jQuery.noConflict(); (function($) { $(function() { // 使用 $ 作为 jQuery 别名的代码 }); })(jQuery); // 其他用 $ 作为别名的库的代码 -------------------------------------------------------------------------------- 创建一个新的别名用以在接下来的库中使用jQuery对象。 jQuery 代码: var j = jQuery.noConflict(); // 基于 jQuery 的代码 j("div p").hide(); // 基于其他库的 $() 代码 $("content").style.display = 'none'; jQuery.noConflict(extreme)jQuery.noConflict(extreme) 将$和jQuery的控制权都交还给原来的库。用之前请考虑清楚! 这是相对于简单的 noConflict 方法更极端的版本,因为这将完全重新定义jQuery。这通常用于一种极端的情况,比如你想要将jQuery嵌入一个高度冲突的环境。注意:调用此方法后极有可能导致插件失效。 -------------------------------------------------------------------------------- Revert control of both the $ and jQuery variables to their original owners. Use with discretion. This is a more-extreme version of the simple noConflict method, as this one will completely undo what jQuery has introduced. This is to be used in an extreme case where you'd like to embed jQuery into a high-conflict environment. NOTE: It's very likely that plugins won't work after this particular method has been called. 返回值 jQuery 参数 extreme (Boolean) : 传入 true 来允许彻底将jQuery变量还原 示例 完全将 jQuery 移到一个新的命名空间。 jQuery 代码: var dom = {}; dom.query = jQuery.noConflict(true); 结果: // 新 jQuery 的代码 dom.query("div p").hide(); // 另一个库 $() 的代码 $("content").style.display = 'none'; // 另一个版本 jQuery 的代码 jQuery("div > p").hide(); 选择器基本 #id#id 根据给定的ID匹配一个元素。 -------------------------------------------------------------------------------- Matches a single element with the given id attribute. 返回值 Element 参数 id (String) : 用于搜索的,通过元素的 id 属性中给定的值 示例 查找 ID 为"myDiv"的元素。 HTML 代码:

id="notMe"

id="myDiv"
jQuery 代码: $("#myDiv"); 结果: [
id="myDiv"
] elementelement 根据给定的元素名匹配所有元素 -------------------------------------------------------------------------------- Matches all elements with the given name. 返回值 Array 参数 element (String) : 一个用于搜索的元素。指向 DOM 节点的标签名。 示例 查找一个 DIV 元素。 HTML 代码:
DIV1
DIV2
SPAN jQuery 代码: $("div"); 结果: [
DIV1
,
DIV2
] .class.class 根据给定的类匹配元素。 -------------------------------------------------------------------------------- Matches all elements with the given class. 返回值 Array 参数 class (String) : 一个用以搜索的类。一个元素可以有多个类,只要有一个符合就能被匹配到。 示例 查找所有类是 "myClass" 的元素. HTML 代码:
div class="notMe"
div class="myClass"
span class="myClass" jQuery 代码: $(".myClass"); 结果: [
div class="myClass"
, span class="myClass" ] ** 匹配所有元素 多用于结合上下文来搜索。 -------------------------------------------------------------------------------- Matches all elements. Most useful when combined with a context to search in. 返回值 Array 示例 找到每一个元素 HTML 代码:
DIV
SPAN

P

jQuery 代码: $("*") 结果: [
DIV
, SPAN,

P

] selector1,selector2,selectorNselector1,selector2,selectorN 将每一个选择器匹配到的元素合并后一起返回。 你可以指定任意多个选择器,并将匹配到的元素合并到一个结果内。 -------------------------------------------------------------------------------- Matches the combined results of all the specified selectors. You can specify any number of selectors to combine into a single result. 返回值 Array 参数 selector1 (Selector) : 一个有效的选择器 selector2 (Selector) : 另一个有效的选择器 selectorN (Selector) : (可选) 任意多个有效选择器 示例 找到匹配任意一个类的元素。 HTML 代码:
div

p class="myClass"

span

p class="notMyClass"

jQuery 代码: $("div,span,p.myClass") 结果: [
div
,

p class="myClass"

, span ] 层级 ancestor descendantancestor descendant 在给定的祖先元素下匹配所有的后代元素 -------------------------------------------------------------------------------- Matches all descendant elements specified by descendant of elements specified by ancestor. 返回值 Array 参数 ancestor (Selector) : 任何有效选择器 descendant (Selector) : 用以匹配元素的选择器,并且它是第一个选择器的后代元素 示例 找到表单中所有的 input 元素 HTML 代码:
jQuery 代码: $("form input") 结果: [ , ] parent > childparent > child 在给定的父元素下匹配所有的子元素 -------------------------------------------------------------------------------- Matches all child elements specified by child of elements specified by parent. 返回值 Array 参数 parent (Selector) : 任何有效选择器 child (Selector) : 用以匹配元素的选择器,并且它是第一个选择器的子元素 示例 匹配表单中所有的子级input元素。 HTML 代码:
jQuery 代码: $("form > input") 结果: [ ] prev + nextprev + next 匹配所有紧接在 prev 元素后的 next 元素 -------------------------------------------------------------------------------- Matches all next elements specified by next that are next to elements specified by prev. 返回值 Array 参数 prev (Selector) : 任何有效选择器 next (Selector) :一个有效选择器并且紧接着第一个选择器 示例 匹配所有跟在 label 后面的 input 元素 HTML 代码:
jQuery 代码: $("label + input") 结果: [ , ] prev ~ siblingsprev ~ siblings 匹配 prev 元素之后的所有 siblings 元素 -------------------------------------------------------------------------------- Matches all sibling elements after the "prev" element that match the filtering "siblings" selector. 返回值 Array 参数 prev (Selector) : 任何有效选择器 siblings (Selector) : 一个选择器,并且它作为第一个选择器的同辈 示例 找到所有与表单同辈的 input 元素 HTML 代码:
jQuery 代码: $("form ~ input") 结果: [ ] 简单 :first:first 匹配找到的第一个元素 -------------------------------------------------------------------------------- Matches the first selected element. 返回值 Element 示例 查找表格的第一行 HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:first") 结果: [ Header 1 ] :last:last 匹配找到的最后一个元素 -------------------------------------------------------------------------------- Matches the last selected element. 返回值 Element 示例 查找表格的最后一行 HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:last") 结果: [ Value 2 ] :not(selector):not(selector) 去除所有与给定选择器匹配的元素 -------------------------------------------------------------------------------- Removes all elements matching the given selector. 返回值 Array 参数 selector (Selector) : 用于筛选的选择器 示例 查找所有未选中的 input 元素 HTML 代码: jQuery 代码: $("input:not(:checked)") 结果: [ ] :even:even 匹配所有索引值为偶数的元素,从 0 开始计数 -------------------------------------------------------------------------------- Matches even elements, zero-indexed. 返回值 Array 示例 查找表格的1、3、5...行(即索引值0、2、4...) HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:even") 结果: [ Header 1, Value 2 ] :odd:odd 匹配所有索引值为奇数的元素,从 0 开始计数 -------------------------------------------------------------------------------- Matches odd elements, zero-indexed. 返回值 Array 示例 查找表格的2、4、6行(即索引值1、3、5...) HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:odd") 结果: [ Value 1 ] :eq(index):eq(index) 匹配一个给定索引值的元素 -------------------------------------------------------------------------------- Matches a single element by its index. 返回值 Element 参数 index (Number) : 从 0 开始计数 示例 查找第二行 HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:eq(1)") 结果: [ Value 1 ] :gt(index):gt(index) 匹配所有大于给定索引值的元素 -------------------------------------------------------------------------------- Matches all elements with an index above the given one. 返回值 Array 参数 index (Number) : 从 0 开始计数 示例 查找第二第三行,即索引值是1和2,也就是比0大 HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:gt(0)") 结果: [ Value 1, Value 2 ] :lt(index):lt(index) 匹配所有小于给定索引值的元素 -------------------------------------------------------------------------------- Matches all elements with an index below the given one. 返回值 Array 参数 index (Number) : 从 0 开始计数 示例 查找第一第二行,即索引值是0和1,也就是比2小 HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:lt(2)") 结果: [ Header 1, Value 1 ] :header:header 匹配如 h1, h2, h3之类的标题元素 -------------------------------------------------------------------------------- Matches all elements that are headers, like h1, h2, h3 and so on. 返回值 Array 示例 给页面内所有标题加上背景色 HTML 代码:

Header 1

Contents 1

Header 2

Contents 2

jQuery 代码: $(":header").css("background", "#EEE"); 结果: [

Header 1

,

Header 2

] :animated:animated 匹配所有没有在执行动画效果中的元素 -------------------------------------------------------------------------------- Matches all elements that are currently being animated. 返回值 Array 示例 只有对不在执行动画效果的元素执行一个动画特效 HTML 代码:
jQuery 代码: $("#run").click(function(){ $("div:not(:animated)").animate({ left: "+20" }, 1000); }); 内容 :contains(text):contains(text) 匹配包含给定文本的元素 -------------------------------------------------------------------------------- Matches elements which contain the given text. 返回值 Array 参数 text (String) : 一个用以查找的字符串 示例 查找所有包含 "John" 的 div 元素 HTML 代码:
John Resig
George Martin
Malcom John Sinclair
J. Ohn jQuery 代码: $("div:contains('John')") 结果: [
John Resig
,
Malcom John Sinclair
] :empty:empty 匹配所有不包含子元素或者文本的空元素 -------------------------------------------------------------------------------- Matches all elements that are empty, be it elements or text. 返回值 Array 示例 查找所有不包含子元素或者文本的空元素 HTML 代码:
Value 1
Value 2
jQuery 代码: $("td:empty") 结果: [ , ] :has(selector):has(selector) 匹配含有选择器所匹配的元素的元素 -------------------------------------------------------------------------------- Matches elements which contain at least one element that matches the specified selector. 返回值 Array 参数 selector (Selector) : 一个用于筛选的选择器 示例 给所有包含 p 元素的 div 元素添加一个 text 类 HTML 代码:

Hello

Hello again!
jQuery 代码: $("div:has(p)").addClass("test"); 结果: [

Hello

] :parent:parent 匹配含有子元素或者文本的元素 -------------------------------------------------------------------------------- Matches all elements that are parents - they have child elements, including text. 返回值 Array 示例 查找所有含有子元素或者文本的 td 元素 HTML 代码:
Value 1
Value 2
jQuery 代码: $("td:parent") 结果: [ Value 1, Value 1 ] 可见性 :hidden:hidden 匹配所有的不可见元素,input 元素的 type 属性为 "hidden" 的话也会被匹配到 -------------------------------------------------------------------------------- Matches all elements that are hidden, or input elements of type "hidden". 返回值 Array 示例 查找所有不可见的 tr 元素 HTML 代码:
Value 1
Value 2
jQuery 代码: $("tr:hidden") 结果: [ Value 1 ] :visible:visible 匹配所有的可见元素 -------------------------------------------------------------------------------- Matches all elements that are visible. 返回值 Array 示例 查找所有可见的 tr 元素 HTML 代码:
Value 1
Value 2
jQuery 代码: $("tr:visible") 结果: [ Value 2 ] 属性 [attribute][attribute] 匹配包含给定属性的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute. 返回值 Array 参数 attribute (String) : 属性名 示例 查找所有含有 id 属性的 div 元素 HTML 代码:

Hello!

jQuery 代码: $("div[id]") 结果: [
] [attribute=value][attribute=value] 匹配给定的属性是某个特定值的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute with a certain value. 返回值 Array 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 属性是 newsletter 的 input 元素 HTML 代码: ' jQuery 代码: $("input[name='newsletter']").attr("checked", true); 结果: [ , ] [attribute!=value][attribute!=value] 匹配给定的属性是不包含某个特定值的元素 -------------------------------------------------------------------------------- Matches elements that don't have the specified attribute with a certain value. 返回值 Array 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 属性不是 newsletter 的 input 元素 HTML 代码: ' jQuery 代码: $("input[name!='newsletter']").attr("checked", true); 结果: [ ] [attribute^=value][attribute^=value] 匹配给定的属性是以某些值开始的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it starts with a certain value. 返回值 Array 参数 attribute (String) : 属性名 value ( String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 以 'news' 开始的 input 元素 HTML 代码: jQuery 代码: $("input[name^='news']") 结果: [ , ] [attribute$=value][attribute$=value] 匹配给定的属性是以某些值结尾的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it ends with a certain value. 返回值 Array 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 以 'letter' 结尾的 input 元素 HTML 代码: jQuery 代码: $("input[name$='letter']") 结果: [ , ] [attribute*=value][attribute*=value] 匹配给定的属性是以包含某些值的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it contains a certain value. 返回值 Array 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 包含 'man' 的 input 元素 HTML 代码: jQuery 代码: $("input[name*='man']") 结果: [ , , ] [selector1][selector2][selectorN][selector1][selector2][selectorN] 复合属性选择器,需要同时满足多个条件时使用。 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it contains a certain value. 返回值 Array 参数 selector1 (Selector) : 属性选择器 selector2 (Selector) : 另一个属性选择器,用以进一步缩小范围 selectorN (Selector) : 任意多个属性选择器 示例 找到所有含有 id 属性,并且它的 name 属性是以 man 结尾的 HTML 代码: jQuery 代码: $("input[id][name$='man']") 结果: [ ] 子元素 :nth-child(index/even/odd/equation):nth-child(index/even/odd/equation) 匹配其父元素下的第N个子或奇偶元素 ':eq(index)' 只匹配一个元素,而这个将为每一个父元素匹配子元素。:nth-child从1开始的,而:eq()是从0算起的! 可以使用: nth-child(even) :nth-child(odd) :nth-child(3n) :nth-child(2) :nth-child(3n+1) :nth-child(3n+2) -------------------------------------------------------------------------------- Matches the nth-child of its parent. While ':eq(index)' matches only a single element, this matches more then one: One for each parent. The specified index is one-indexed, in contrast to :eq() which starst at zero. 返回值 Array 参数 index (Number) : 要匹配元素的序号,从1开始 示例 在每个 ul 查找第 2 个li HTML 代码:
  • John
  • Karl
  • Brandon
  • Glen
  • Tane
  • Ralph
jQuery 代码: $("ul li:nth-child(2)") 结果: [
  • Karl
  • ,
  • Tane
  • ] :first-child:first-child 匹配第一个子元素 ':first' 只匹配一个元素,而此选择符将为每个父元素匹配一个子元素 -------------------------------------------------------------------------------- Matches the first child of its parent. While ':first' matches only a single element, this matches more then one: One for each parent. 返回值 Array 示例 在每个 ul 中查找第一个 li HTML 代码:
    • John
    • Karl
    • Brandon
    • Glen
    • Tane
    • Ralph
    jQuery 代码: $("ul li:first-child") 结果: [
  • John
  • ,
  • Glen
  • ] :last-child:last-child 匹配最后一个子元素 ':last'只匹配一个元素,而此选择符将为每个父元素匹配一个子元素 -------------------------------------------------------------------------------- Matches the last child of its parent. While ':last' matches only a single element, this matches more then one: One for each parent. 返回值 Array 示例 在每个 ul 中查找最后一个 li HTML 代码:
    • John
    • Karl
    • Brandon
    • Glen
    • Tane
    • Ralph
    jQuery 代码: $("ul li:last-child") 结果: [
  • Brandon
  • ,
  • Ralph
  • ] :only-child:only-child 如果某个元素是父元素中唯一的子元素,那将会被匹配 如果父元素中含有其他元素,那将不会被匹配。 -------------------------------------------------------------------------------- Matches the only child of its parent. If the parent has other child elements, nothing is matched. 返回值 Array 示例 在 ul 中查找是唯一子元素的 li HTML 代码:
    • John
    • Karl
    • Brandon
    css(name) 访问第一个匹配元素的样式属性。 -------------------------------------------------------------------------------- Return a style property on the first matched element. 返回值 String 参数 name (String) : 要访问的属性名称 示例 取得第一个段落的color样式属性的值。 jQuery 代码: $("p").css("color"); toggle(fn,fn) 每次点击时切换要调用的函数。 如果点击了一个匹配的元素,则触发指定的第一个函数,当再次点击同一元素时,则触发指定的第二个函数。随后的每次点击都重复对这两个函数的轮番调用。 可以使用unbind("click")来删除。 -------------------------------------------------------------------------------- Toggle between two function calls every other click. Whenever a matched element is clicked, the first specified function is fired, when clicked again, the second is fired. All subsequent clicks continue to rotate through the two functions. Use unbind("click") to remove. 返回值 jQuery 参数 fn (Function) : 第奇数次点击时要执行的函数。 fn (Function) : 第偶数次点击时要执行的函数。 示例 对表格的切换一个类 jQuery 代码: $("td").toggle( function () { $(this).addClass("selected"); }, function () { $(this).removeClass("selected"); } ); ajaxSuccess(callback) AJAX 请求成功时执行函数。Ajax 事件。 XMLHttpRequest 对象和设置作为参数传递给回调函数。 -------------------------------------------------------------------------------- Attach a function to be executed whenever an AJAX request completes successfully. This is an Ajax Event. The XMLHttpRequest and settings used for that request are passed as arguments to the callback. 返回值 jQuery 参数 callback (Function) : 待执行函数 示例 当 AJAX 请求成功后显示消息。 jQuery 代码: $("#msg").ajaxSuccess(function(evt, request, settings){ $(this).append("
  • Successful Request!
  • "); }); jQuery.ajax(options) 通过 HTTP 请求加载远程数据。 jQuery 底层 AJAX 实现。简单易用的高层实现见 $.get, $.post 等。 $.ajax() 返回其创建的 XMLHttpRequest 对象。大多数情况下你无需直接操作该对象,但特殊情况下可用于手动终止请求。 注意: 如果你指定了 dataType 选项,请确保服务器返回正确的 MIME 信息,(如 xml 返回 "text/xml")。错误的 MIME 类型可能导致不可预知的错误。见 Specifying the Data Type for AJAX Requests 。 $.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数信息。详细参数选项见下。 jQuery 1.2 中,您可以跨域加载 JSON 数据,使用时需将数据类型设置为 JSONP。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。数据类型设置为 "jsonp" 时,jQuery 将自动调用回调函数。 -------------------------------------------------------------------------------- Load a remote page using an HTTP request. This is jQuery's low-level AJAX implementation. See $.get, $.post etc. for higher-level abstractions that are often easier to understand and use, but don't offer as much functionality (such as error callbacks). $.ajax() returns the XMLHttpRequest that it creates. In most cases you won't need that object to manipulate directly, but it is available if you need to abort the request manually. Note: If you specify the dataType option described below, make sure the server sends the correct MIME type in the response (eg. xml as "text/xml"). Sending the wrong MIME type can lead to unexpected problems in your script. See Specifying the Data Type for AJAX Requests for more information. $.ajax() takes one argument, an object of key/value pairs, that are used to initialize and handle the request. See below for a full list of the key/values that can be used. As of jQuery 1.2, you can load JSON data located on another domain if you specify a JSONP callback, which can be done like so: "myurl?callback=?". jQuery automatically replaces the ? with the correct method name to call, calling your specified callback. Or, if you set the dataType to "jsonp" a callback will be automatically added to your Ajax request. 返回值 XMLHttpRequest 参数 options (可选) : AJAX 请求设置。所有选项都是可选的。 选项 async (Boolean) : (默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。 beforeSend (Function) : 发送请求前可修改 XMLHttpRequest 对象的函数,如添加自定义 HTTP 头。XMLHttpRequest 对象是唯一的参数。 Ajax Event. cache (Boolean) : (默认: true) jQuery 1.2 新功能,设置为 false 将不会从浏览器缓存中加载请求信息。 complete (Function) : 请求完成后回调函数 (请求成功或失败时均调用)。参数: XMLHttpRequest 对象,成功信息字符串。 Ajax 事件。 contentType (String) : (默认: "application/x-www-form-urlencoded") 发送信息至服务器时内容编码类型。默认值适合大多数应用场合。 data (Object,String) : 发送到服务器的数据。将自动转换为请求字符串格式。GET 请求中将附加在 URL 后。查看 processData 选项说明以禁止此自动转换。必须为 Key/Value 格式。如果为数组,jQuery 将自动为不同值对应同一个名称。如 {foo:["bar1", "bar2"]} 转换为 '&foo=bar1&foo=bar2'。 dataType (String) : 预期服务器返回的数据类型。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息返回 responseXMLresponseText,并作为回调函数参数传递,可用值: "xml": 返回 XML 文档,可用 jQuery 处理。 "html": 返回纯文本 HTML 信息;包含 script 元素。 "script": 返回纯文本 JavaScript 代码。不会自动缓存结果。 "json": 返回 JSON 数据 。 "jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。 error (Function) : (默认: 自动判断 (xml 或 html)) 请求失败时调用时间。参数:XMLHttpRequest 对象,错误信息,(可能)捕获的错误对象。Ajax 事件。 global (Boolean) : (默认: true) 是否触发全局 AJAX 事件。设置为 false 将不会触发全局 AJAX 事件,如 ajaxStart 或 ajaxStop 可用于控制不同的 Ajax 事件。 ifModified (Boolean) : (默认: false) 仅在服务器数据改变时获取新数据。使用 HTTP 包 Last-Modified 头信息判断。 processData (Boolean) : (默认: true) 默认情况下,发送的数据将被转换为对象(技术上讲并非字符串) 以配合默认内容类型 "application/x-www-form-urlencoded"。如果要发送 DOM 树信息或其它不希望转换的信息,请设置为 false。 success (Function) : 请求成功后回调函数。参数:服务器返回数据,数据格式。 Ajax 事件。 timeout (Number) : 设置请求超时时间(毫秒)。此设置将覆盖全局设置。 type (String) : (默认: "GET") 请求方式 ("POST" 或 "GET"), 默认为 "GET"。注意:其它 HTTP 请求方法,如 PUT 和 DELETE 也可以使用,但仅部分浏览器支持。 url (String) : (默认: 当前页地址) 发送请求的地址。 示例 加载并执行一个 JS 文件。 jQuery 代码: $.ajax({ type: "GET", url: "test.js", dataType: "script" }); -------------------------------------------------------------------------------- 保存数据到服务器,成功时显示信息。 jQuery 代码: $.ajax({ type: "POST", url: "some.php", data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg ); } }); -------------------------------------------------------------------------------- 装入一个 HTML 网页最新版本。 jQuery 代码: $.ajax({ url: "test.html", cache: false, success: function(html){ $("#results").append(html); } }); -------------------------------------------------------------------------------- 同步加载数据。发送请求时锁住浏览器。需要锁定用户交互操作时使用同步方式。 jQuery 代码: var html = $.ajax({ url: "some.php", async: false }).responseText; -------------------------------------------------------------------------------- 发送 XML 数据至服务器。设置 processData 选项为 false,防止自动转换数据格式。 jQuery 代码: var xmlDocument = [create xml document]; $.ajax({ url: "page.php", processData: false, data: xmlDocument, success: handleResponse });
    选择器速度提升
    选择器的速度大幅度提高了,下表为jQuery1.1.2和1.1.3的选择器速度对比,提高了8倍多
    Browser jQuery 1.1.2 jQuery 1.1.3 % Improvement
    IE 6 4890ms 661ms 740%
    Firefox 2 5629ms 567ms 993%
    Safari 2 3575ms 475ms 753%
    Opera 9.1 3196ms 326ms 980%
    Average improvement: 867%

    下表为jQuery1.1.3与常用的一些JS库选择器的对比:
    Browser Prototype jQuery Mootools Ext Dojo
    IE 6 1476ms 661ms 1238ms 672ms 738ms
    Firefox 2 219ms 567ms 220ms 951ms 440ms
    Safari 2 1568ms 475ms 909ms 417ms 527ms
    Opera 9.1 220ms 326ms 217ms 296ms 220ms


    更新项目


    New Selectors
    Unicode Selectors: This is a huge addition for those of you who want to use Unicode attribute values, IDs, class names, or tag names. You can now use them directly in jQuery selectors:

    $("div.台北")
    $("div#台北")
    $("foo_bar台北")
    $("div[@id=台北]")Escape Selectors: A frequently requested feature you can now select elements by ID (or other selector) that uses a special character, for example this will find the div that has the ID of “foo.bar”:

    $("div#foo\\.bar")Inequality Selector: While this selector isn’t part of the CSS specification, it’s frequently used and included in other selector libraries, so we decided to add it in:

    $("div[@id!=test]"):nth-child() improvements: This selector allows you to locate specific child elements. We’ve supported selectors like :nth-child(1) and :nth-child(odd) since the beginning of jQuery, now we’ve added advanced :nth-child selectors, such as:

    $("div:nth-child(2n)")
    $("div:nth-child(2n+1)")
    $("div:nth-child(n)")Space-separated attributes: After being removed in jQuery 1.0, this selector has now been brought back by popular demand. It allows you to locate individual items in a space-separated attribute (such as a class or rel attribute).

    $("a[@rel~=test]")Animation Improvements

    参数:
    options
    返回值:
    XMLHttpRequest
    使用HTTP请求一个页面。
    这是jQuery的低级AJAX实现。要查看高级抽象,见$.set、$.post等,这些方法更易于理解和使用。但是功能上有限制(例如,没有错误处理函数)。
    警告:如果数据类型指定为"script",那么POST自动转化为GET方法。(因为script会作为一个嵌入页面的script标签进行载入)
    $.ajax()函数返回它创建的XMLHttpRequest对象。在大部分情况下,你不需要直接操作此对象。通常,这个XMLHttpRequest对象主要用于需要手动中断XMLHttpRequest请求的时候。
    注意:如果你指明了下面列出的数据类型,请确保服务端发送了正确的MIME响应类型(如. xml 的类型是 "text/xml")。错误的MIME类型能够导致脚本出现意想不到的问题。请查看AJAX的范例来了解数据类型的更多信息。
    $.ajax()函数需要一个参数,一个包含有键/值对的对象,用于初始化并操作请求对象。
    在jQuery 1.2版本中,如果你指明了一个JSONP回调函数,你就可以从其它的域中载入JSON类型的数据,写法类似于 "myurl?callback=?" 。jQuery会自动调用正确的方法名称来代替查询字符串,执行你指定的回调函数。或者,你也可以指定jsonp的数据类型的回调函数,此函数会自动添加到Ajax请求中。
    参数选项:
    async(true) 数据类型: Boolean
    默认情况下,所有的请求都是异步发送的(默认为true)。 如果需要发送同步请求, 设置选项为false。注意,同步请求可能会暂时的锁定浏览器, 当请求激活时不能进行任何操作。
    beforeSend 数据类型: Function
    一个预处理函数用于在发送前修改XMLHttpRequest对象,设置自定义头部等。 XMLHttpRequest作为惟一的参数被传递。这是一个 Ajax 事件。 function (XMLHttpRequest) {
    this; // the options for this ajax request
    }cache(true) 数据类型: Boolean
    jQuery 1.2中新添加的参数, 如果设为false,则会强制浏览器不缓存请求的页面。
    complete 数据类型: Function
    当请求完成时执行的函数(在成功或失败之后执行)。这个函数有2个参数: XMLHttpRequest对象和一个描述HTTP相应的状态字符串。 这是一个 Ajax 事件。 function (XMLHttpRequest, textStatus) {
    this; // the options for this ajax request
    }contentType("application/x-www-form-urlencoded") 数据类型: String
    发送到服务器的数据的内容类型。默认是 "application/x-www-form-urlencoded", 适合大多数情况。
    data 数据类型: Object,String
    要发送给服务器的数据。如果不是字符串,那么它会被转化为一个查询字符串。在GET请求中它被添加到url的末尾。要防止这种自动转化,请查看processData选项。 数据对象必须是一组键/值对。如果键对应的值是数组,jQuery会将其值赋给同一个键属性。 例如 {foo:["bar1", "bar2"]} 变为 '&foo=bar1&foo=bar2'。
    dataType( Intelligent Guess (xml or html)) 数据类型: String
    期待由服务器返回值类型。如果没有明确指定,jQuery会根据实际返回的MIME类型自动的将responseXMLresponseText传递给success指定的回调函数。有效的类型(返回的类型的结果值会作为第一个参数传递给success指定的回调函数)有: "xml": 返回一个可以由jQuery处理的XML文档。
    "html": 返回文本格式的HTML代码。包括求值后的脚本标记。
    "script": 将响应作为Javascript语句求值,并返回纯文本。不缓存此脚本,除非设置了cache选项。设置为"script"类型会将post方法转换为get方法。
    "json": 将响应作为JSON求值,并返回一个Javascript对象。
    "jsonp": 使用JSONP载入一个JSON代码块. 会在URL的末尾添加"?callback=?"来指明回调函数。(jQuery 1.2以上的版本支持)
    "text": 文本格式的字符串
    error 数据类型: Function
    请求失败时执行的函数。函数具有3个参数: XMLHttpRequest对象,一个描述产生的错误类型和一个可选的异常对象, 如果有的化。 这是一个Ajax 事件。function (XMLHttpRequest, textStatus, errorThrown) {
    // typically only one of textStatus or errorThrown
    // will have info
    this; // the options for this ajax request
    }global(true) 数据类型: Boolean
    是否为当前的请求触发全局AJAX事件处理函数,默认值为true。设置为false可以防止触发像ajaxStart或ajaxStop这样的全局事件处理函数。这可以用于控制多个不同的Ajax事件。
    ifModified(false) 数据类型: Boolean
    只有响应自上次请求后被修改过才承认是成功的请求。是通过检查头部的Last-Modified值实现的。默认值为false,即忽略对头部的检查
    jsonp 数据类型: String
    在jsonp请求中重新设置回调的函数。这个值用于代替'callback=?'中的查询字符串。'callback=?'位于get请求中url的末尾或是post请求传递的数据中。因此设置 {jsonp:'onJsonPLoad'} 会将 'onJsonPLoad=?' 传送给服务器。
    processData(true) 数据类型: Boolean
    在默认的情况下,如果data选项传进的数据是一个对象而不是字符串,将会自动地被处理和转换成一个查询字符串,以适应默认的content-type--"application/x-www-form-urlencoded"。如果想发送DOMDocuments,就要把这个选项设置为false。
    success 数据类型: Function
    当请求成功时调用的函数。这个函数会得到二个参数:从服务器返回的数据(根据“dataType”进行了格式化)和一个描述HTTP相应的状态字符串。这是一个 Ajax 事件。 function (data, textStatus) {
    // data could be xmlDoc, jsonObj, html, text, etc...
    this; // the options for this ajax request
    }timeout 数据类型: Number
    如果通过$.ajaxSetup设置了一个全局timeout,那么此函数使用一个局部timeout覆盖了全局timeout(单位为毫秒)。例如,你可以设置比较长的延迟给一个特殊的请求,同时其他所有请求使用1秒的延迟。有关全局延迟,见$.ajaxTimeout()。
    type("GET") 数据类型: String
    请求的类型 ("POST" 或 "GET"), 默认是 "GET"。注意:其他的HTTP请求方法,如PUT和DELETE,在这里也可以使用,当时它们并不被所有的浏览器支持。
    url(The current page) 数据类型: String
    请求发送的目标URL地址
    username 数据类型: String
    username可用于在响应一个HTTP连接时的认证请求。 实例
    载入并执行一个JavaScript文件。
    $.ajax({
    type: "GET",
    url: "test.js",
    dataType: "script"
    });保存数据到服务器,完成后通知用户。
    $.ajax({
    type: "POST",
    url: "some.php",
    data: "name=John&location=Boston",
    success: function(msg){
    alert( "Data Saved: " + msg );
    }
    });取得一个HTML页面的最新版本。
    $.ajax({
    url: "test.html",
    cache: false,
    success: function(html){
    $("#results").append(html);
    }
    });同步载入数据。在执行请求的时候阻塞浏览器。这是在保证数据的同步性比交互更重要的情况下的一种更好的方法。
    var html = $.ajax({
    url: "some.php",
    async: false
    }).responseText;向服务器发送xml文档数据。通过设置processData选项为false,将数据自动转换为string的动作被禁止了。
    var xmlDocument = [create xml document];
    $.ajax({
    url: "page.php",
    processData: false,
    data: xmlDocument,
    success: handleResponse
    });load( url, [data], [callback] )参数:
    url (String): 装入页面的URL地址。
    params (Map): (可选)发送到服务端的键/值对参数。
    callback (Function): (可选) 当数据装入完成时执行的函数. function (responseText, textStatus, XMLHttpRequest) {
    this; // dom element
    }返回值:
    jQuery
    装入一个远程HTML内容到一个DOM结点。 默认使用get方法发送请求,但如果指定了额外的参数,将会使用post方法发送请求。在 jQuery 1.2中,可以在URL参数中指定一个jQuery选择器,这会过滤返回的HTML文档,只取得文档中匹配选择器的元素。此语法类似于"url #some > selector"。
    实例
    载入文档的sidebar的导航部分到一个无序列表中。
    $("#links").load("/Main_Page #p-Getting-Started li");将feeds.html文件载入到id为feeds的div中。
    $("#feeds").load("feeds.html");同上,但是发送了附加的参数,并且在响应结束后执行一个自定义函数。
    $("#feeds").load("feeds.php", {limit: 25}, function(){
    alert("The last 25 entries in the feed have been loaded");
    });jQuery.get( url, [data], [callback] )参数:
    url (String): 装入页面的URL地址
    Map(可选): (可选)发送到服务端的键/值对参数
    callback (Function): (可选) 当远程页面装入完成时执行的函数 function (data, textStatus) {
    // data可以是xmlDoc, jsonObj, html, text, 等...
    this; // the options for this ajax request
    }返回值:
    XMLHttpRequest
    使用GET请求一个页面。
    这是向服务器发送get请求的简单方法。它可以指定一个回调函数,在请求完成后执行(只有在请求成功时)。如果还需要设置error和success回调函数,则需要使用$.ajax。
    实例
    请求test.php页,忽略返回值.
    $.get("test.php");请求test.php页并发送附加数据(忽略返回值).
    $.get("test.php", { name: "John", time: "2pm" } );显示从test.php请求的返回值(HTML 或 XML, 根据不同返回值).
    $.get("test.php", function(data){
    alert("Data Loaded: " + data);
    });显示向test.cgi发送附加数据请求的返回值 (HTML 或 XML, 根据不同返回值).
    $.get("test.cgi", { name: "John", time: "2pm" },
    function(data){
    alert("Data Loaded: " + data);
    });jQuery.getJSON( url, [data], [callback] )参数:
    url (String): 装入页面的URL地址
    Map(可选): (可选)发送到服务端的键/值对参数
    callback (Function): (可选) 当数据装入完成时执行的函数 function (data, textStatus) {
    // data will be a jsonObj
    this; // the options for this ajax request
    }返回值:
    XMLHttpRequest
    使用GET请求JSON数据。
    在jQuery 1.2版本中,如果你指明了一个JSONP回调函数,你就可以从其它的域中载入JSON类型的数据,写法类似于 "myurl?callback=?" 。jQuery会自动调用正确的方法名称来代替查询字符串,执行你指定的回调函数。或者,你也可以指定jsonp的数据类型的回调函数,此函数会自动添加到Ajax请求中。注意: 请记住, that lines after this function will be executed before callback.
    实例
    从Flickr JSONP API中载入最新的四幅猫的图片
    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
    function(data){
    $.each(data.items, function(i,item){
    $("").attr("src", item.media.m).appendTo("#images");
    if ( i == 3 ) return false;
    });
    });从test.js载入JSON数据, 从返回的JSON数据读取name值。
    $.getJSON("test.js", function(json){
    alert("JSON Data: " + json.users[3].name);
    });从test.js载入JSON数据, 传递一个附加参数,从返回的JSON数据读取name值。
    $.getJSON("test.js", { name: "John", time: "2pm" }, function(json){
    alert("JSON Data: " + json.users[3].name);
    });显示向test.php发送请求的返回值 (HTML 或 XML, 根据不同返回值).
    $.getIfModified("test.php", function(data){
    alert("Data Loaded: " + data);
    });显示向test.php发送请求的返回值 (HTML 或 XML, 根据不同返回值),提供了一个附加的参数.
    $.getIfModified("test.php", { name: "John", time: "2pm" },
    function(data){
    alert("Data Loaded: " + data);
    });列出从pages.php返回的查询结果,将返回的数组转化为一段HTML代码。
    var id=$("#id").attr("value");
    $.getJSON("pages.php",{id:id},dates);
    function dates(datos)
    {
    $("#list").html("Name:"+datos[1].name+"
    "+"Last Name:"+datos[1].lastname+"
    "+"Address:"+datos[1].address);
    }jQuery.getScript( url, [callback] )参数:
    url (String): 装入页面的URL地址
    callback (Function): (可选) 当数据装入完成时执行的函数 function (data, textStatus) {
    // data应该是javascript
    this; // the options for this ajax request
    }返回值:
    XMLHttpRequest
    使用GET请求JavaScript文件并执行。
    在jQuery 1.2前, getScript只能从页面所在的主机载入脚本,1.2中, 你可以从任何主机载入脚本。警告: Safari 2 及其更老的版本不能在全局上下文中正确识别脚本。如果你通过getScript载入函数,请保证设置一个延迟来执行这个脚本。
    实例
    我们动态的载入一个新的官方jQuery颜色动画插件,载入后绑定一些动画效果到元素上。
    $.getScript("http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js", function(){
    $("#go").click(function(){
    $(".block").animate( { backgroundColor: 'pink' }, 1000)
    .animate( { backgroundColor: 'blue' }, 1000);
    });
    });载入test.js JavaScript文件并执行。
    $.getScript("test.js");载入test.js JavaScript文件并执行,当执行结束后显示一条警告信息。
    $.getScript("test.js", function(){
    alert("Script loaded and executed.");
    });jQuery.post( url, [data], [callback], [type] )参数:
    url (String): 装入页面的URL地址
    Map(可选): (可选)发送到服务端的键/值对参数
    callback (Function): (可选) 当数据装入完成时执行的函数 function (data, textStatus) {
    // data可能是 xmlDoc, jsonObj, html, text, 等...
    this; // the options for this ajax request
    }String $.postJSON = function(url, data, callback) {
    $.post(url, data, callback, "json");
    };返回值:
    XMLHttpRequest
    使用POST请求一个页面。
    这是向服务器发送post请求的简单方法。它可以指定一个回调函数,在请求完成后执行(只有在请求成功时)。如果还需要设置error和success回调函数,则需要使用$.ajax。
    ajaxComplete( callback )参数:
    callback (Function): 要执行的函数 function (event, XMLHttpRequest, ajaxOptions) {
    this; // dom element listening
    }返回值:
    jQuery
    当一个AJAX请求结束后,执行一个函数。这是一个Ajax事件
    实例
    当AJAX请求完成时显示一条信息。
    $("#msg").ajaxComplete(function(request, settings){
    $(this).append("
  • Request Complete.
  • ");
    });ajaxError( callback )参数:
    callback (Function): 要执行的函数 function (event, XMLHttpRequest, ajaxOptions, thrownError) {
    // thrownError only passed if an error was caught
    this; // dom element listening
    }返回值:
    jQuery
    当一个AJAX请求失败后,执行一个函数。这是一个Ajax事件.
    实例
    当AJAX请求错误时显示一条信息。
    $("#msg").ajaxError(function(request, settings){
    $(this).append("
  • Error requesting page " + settings.url + "
  • ");
    });ajaxSend( callback )参数:
    callback (Function): 要执行的函数 function (event, XMLHttpRequest, ajaxOptions) {
    this; // dom element listening
    }返回值:
    jQuery
    在一个AJAX请求发送时,执行一个函数。这是一个Ajax事件.
    实例
    当AJAX请求发出后显示一条信息。
    $("#msg").ajaxSend(function(evt, request, settings){
    $(this).append(" });ajaxStart( callback )参数:
    callback (Function): 要执行的函数 function () {
    this; // dom element listening
    }返回值:
    jQuery
    在一个AJAX请求开始但还没有激活时,执行一个函数。这是一个Ajax事件.
    实例
    当AJAX请求开始(并还没有激活时)显示loading信息。
    $("#loading").ajaxStart(function(){
    $(this).show();
    });ajaxStop( callback )参数:
    callback (Function): 要执行的函数 function () {
    this; // dom element listening
    }返回值:
    jQuery
    当所有的AJAX都停止时,执行一个函数。这是一个Ajax事件.
    实例
    当所有AJAX请求都停止时,隐藏loading信息。
    $("#loading").ajaxStop(function(){
    $(this).hide();
    });ajaxSuccess( callback )参数:
    callback (Function): 要执行的函数 function (event, XMLHttpRequest, ajaxOptions) {
    this; // dom element listening
    }返回值:
    jQuery
    当一个AJAX请求成功完成后,执行一个函数。这是一个Ajax事件
    实例
    当AJAX请求成功完成时,显示信息。
    $("#msg").ajaxSuccess(function(evt, request, settings){
    $(this).append("
  • Successful Request!
  • ");
    });jQuery.ajaxSetup( options )参数:
    Options: 用于Ajax请求的键/值对
    为所有的AJAX请求进行全局设置。查看$.ajax函数取得所有选项信息。
    实例
    设置默认的全局AJAX请求选项。
    $.ajaxSetup({
    url: "/xmlhttp/",
    global: false,
    type: "POST"
    });
    $.ajax({ data: myData });serialize( )返回值:
    jQuery
    以名称和值的方式连接一组input元素。返回值类似于: single=Single2&multiple=Multiple&multiple=Multiple3&radio=radio2 。在jQuery 1.2中。serialize方法实现了正确表单元素序列,而不再需要插件支持。
    实例
    连接表单元素的一组查询字符串,可用于发送Ajax请求。
    function showValues() {
    var str = $("form").serialize();
    $("#results").text(str);
    }

    $(":checkbox, :radio").click(showValues);
    $("select").change(showValues);
    showValues();serializeArray( )返回值:
    jQuery
    连接所有的表单和表单元素(类似于.serialize()方法),但是返回一个JSON数据格式。
    实例
    从form中取得一组值,显示出来
    function showValues() {
    var fields = $(":input").serializeArray();
    alert(fields);
    $("#results").empty();
    jQuery.each(fields, function(i, field){
    $("#results").append(field.value + " ");
    });
    }

    $(":checkbox, :radio").click(showValues);
    $("select").change(showValues);
    showValues();

    21,887

    社区成员

    发帖
    与我相关
    我的任务
    社区描述
    从PHP安装配置,PHP入门,PHP基础到PHP应用
    社区管理员
    • 基础编程社区
    加入社区
    • 近7日
    • 近30日
    • 至今
    社区公告
    暂无公告

    试试用AI创作助手写篇文章吧