问题排查:
1、看xmlHttpRequest引擎对象的responseXML属性中是否有内容,方法为:alert(myXmlHttpRequest.responseXML);结果返回为null。说明此属性中没有接收到服务器返回的数据信息。但是换成alert(myXmlHttpRequest.responseText);时,却返回“<province><city>西安市</city><city>渭南市</city></procince>”这一字符串。
至此,解决方案已经出来了,因为responseText属性中有内容,但是responseXML属性中没有内容,说明头信息的说明中出现问题了。
原来写成了header("content-type:text/text;charset=utf-8");,这样数据必然以文本形式返回至responseText属性中了。
应该写成header("content-type:text/xml;charset=utf-8");,这样的话,数据才能以XML的形式返回到responseXML属性中。
唉,粗心导致的问题啊!
今天的教训是:如果出现了问题,应该首先想到,在本程序中,与此问题有关的代码是否有问题。
2、还有一个注意的地方,应该是getElementsByTagName(),一定不能忘记s。
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 代码:
<p>one</p> <div><p>two</p></div> <p>three</p>
jQuery 代码:
$("div > p");
[ <p>two</p> ]
--------------------------------------------------------------------------------
在文档的第一个表单中,查找所有的单选按钮(即: type 值为 radio 的 input 元素)。
jQuery 代码:
$("input:radio", document.forms[0]);
--------------------------------------------------------------------------------
在一个由
AJAX
返回
的
XML
文档中,查找所有的 div 元素。
jQuery 代码:
$("div",
xml
.
response
XML
);
jQuery(html)jQuery(html)
根据提供的原始 HTML 标记字符串,动态创建由 jQuery 对象包装的 DOM 元素。
你可以传递一个手写的 HTML 字符串,或者由某些模板引擎或插件创建的字符串,也可以是通过
AJAX
加载过来的字符串。但是在你创建 input 元素的时会有限制,可以参考第二个示例。当然这个字符串可以包含斜杠 (比如一个图像地址),还有反斜杠。当你创建单个元素时,请使用闭合标签或 XHTML 格式。例如,创建一个 span ,可以用 $("<span/>") 或 $("<span></span>") ,但不推荐 $("<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 $("<span/>") or $("<span></span>") instead of without the closing slash/tag.
jQuery
html (String) : 用于动态创建DOM元素的HTML标记字符串
动态创建一个 div 元素(以及其中的所有内容),并将它追加到 body 元素中。在这个函数的内部,是通过临时创建一个元素,并将这个元素的 innerHTML 属性设置为给定的标记字符串,来实现标记到 DOM 元素转换的。所以,这个函数既有灵活性,也有局限性。
jQuery 代码:
$("<div><p>Hello</p></div>").appendTo("body");
--------------------------------------------------------------------------------
创建一个 <input> 元素必须同时设定 type 属性。因为微软规定 <input> 元素的 type 只能写一次。
jQuery 代码:
// 在 IE 中无效:
$("<input>").attr("type", "checkbox");
// 在 IE 中有效:
$("<input type='checkbox'>");
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<Element>) : 用于封装成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 代码:
<img/><img/>
jQuery 代码:
$("img").each(function(i){
this.src = "test" + i + ".jpg";
[ <img src="test0.jpg" />, <img src="test1.jpg" /> ]
--------------------------------------------------------------------------------
如果你想得到 jQuery对象,可以使用 $(this) 函数。
jQuery 代码:
$("img").each(function(){
$(this).toggleClass("example");
--------------------------------------------------------------------------------
你可以使用 'return' 来提前跳出 each() 循环。
HTML 代码:
<button>Change colors</button>
<span></span>
<div></div>
<div></div>
<div></div>
<div></div>
<div id="stop">Stop here</div>
<div></div>
<div></div>
<div></div>
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 代码:
<img src="test1.jpg"/> <img src="test2.jpg"/>
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 代码:
<img src="test1.jpg"/> <img src="test2.jpg"/>
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<Element>
选择文档中所有图像作为元素数组,并用数组内建的 reverse 方法将数组反向。
HTML 代码:
<img src="test1.jpg"/> <img src="test2.jpg"/>
jQuery 代码:
$("img").get().reverse();
[ <img src="test2.jpg"/> <img src="test1.jpg"/> ] 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 代码:
<img src="test1.jpg"/> <img src="test2.jpg"/>
jQuery 代码:
$("img").get(0);
[ <img src="test1.jpg"/> ] 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 代码:
<div id="foobar"><b></b><span id="foo"></span></div>
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 代码:
<div id="notMe"><p>id="notMe"</p></div>
<div id="myDiv">id="myDiv"</div>
jQuery 代码:
$("#myDiv");
[ <div id="myDiv">id="myDiv"</div> ] elementelement
根据给定的元素名匹配所有元素
--------------------------------------------------------------------------------
Matches all
elements
with the given name.
Array<Element>
element (String) : 一个用于搜索的元素。指向 DOM 节点的标签名。
查找一个 DIV 元素。
HTML 代码:
<div>DIV1</div>
<div>DIV2</div>
<span>SPAN</span>
jQuery 代码:
$("div");
[ <div>DIV1</div>, <div>DIV2</div> ] .class.class
根据给定的类匹配元素。
--------------------------------------------------------------------------------
Matches all
elements
with the given class.
Array<Element>
class (String) : 一个用以搜索的类。一个元素可以有多个类,只要有一个符合就能被匹配到。
查找所有类是 "myClass" 的元素.
HTML 代码:
<div class="notMe">div class="notMe"</div>
<div class="myClass">div class="myClass"</div>
<span class="myClass">span class="myClass"</span>
jQuery 代码:
$(".myClass");
[ <div class="myClass">div class="myClass"</div>, <span class="myClass">span class="myClass"</span> ] **
匹配所有元素
多用于结合上下文来搜索。
--------------------------------------------------------------------------------
Matches all
elements
.
Most useful when combined with a context to search in.
Array<Element>
找到每一个元素
HTML 代码:
<div>DIV</div>
<span>SPAN</span>
<p>P</p>
jQuery 代码:
$("*")
[ <div>DIV</div>, <span>SPAN</span>, <p>P</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<Element>
selector1 (Selector) : 一个有效的选择器
selector2 (Selector) : 另一个有效的选择器
selectorN (Selector) : (可选) 任意多个有效选择器
找到匹配任意一个类的元素。
HTML 代码:
<div>div</div>
<p class="myClass">p class="myClass"</p>
<span>span</span>
<p class="notMyClass">p class="notMyClass"</p>
jQuery 代码:
$("div,span,p.myClass")
[ <div>div</div>, <p class="myClass">p class="myClass"</p>, <span>span</span> ] 层级
ancestor descendantancestor descendant
在给定的祖先元素下匹配所有的后代元素
--------------------------------------------------------------------------------
Matches all descendant
elements
specified by descendant of
elements
specified by ancestor.
Array<Element>
ancestor (Selector) : 任何有效选择器
descendant (Selector) : 用以匹配元素的选择器,并且它是第一个选择器的后代元素
找到表单中所有的 input 元素
HTML 代码:
<label>Name:</label>
<input name="name" />
<fieldset>
<label>Newsletter:</label>
<input name="newsletter" />
</fieldset>
</form>
<input name="none" />
jQuery 代码:
$("form input")
[ <input name="name" />, <input name="newsletter" /> ] parent > childparent > child
在给定的父元素下匹配所有的子元素
--------------------------------------------------------------------------------
Matches all child
elements
specified by child of
elements
specified by parent.
Array<Element>
parent (Selector) : 任何有效选择器
child (Selector) : 用以匹配元素的选择器,并且它是第一个选择器的子元素
匹配表单中所有的子级input元素。
HTML 代码:
<label>Name:</label>
<input name="name" />
<fieldset>
<label>Newsletter:</label>
<input name="newsletter" />
</fieldset>
</form>
<input name="none" />
jQuery 代码:
$("form > input")
[ <input name="name" /> ] prev + nextprev + next
匹配所有紧接在 prev 元素后的 next 元素
--------------------------------------------------------------------------------
Matches all next
elements
specified by next that are next to
elements
specified by prev.
Array<Element>
prev (Selector) : 任何有效选择器
next (Selector) :一个有效选择器并且紧接着第一个选择器
匹配所有跟在 label 后面的 input 元素
HTML 代码:
<label>Name:</label>
<input name="name" />
<fieldset>
<label>Newsletter:</label>
<input name="newsletter" />
</fieldset>
</form>
<input name="none" />
jQuery 代码:
$("label + input")
[ <input name="name" />, <input name="newsletter" /> ] prev ~ siblingsprev ~ siblings
匹配 prev 元素之后的所有 siblings 元素
--------------------------------------------------------------------------------
Matches all sibling
elements
after the "prev" element that match the filtering "siblings" selector.
Array<Element>
prev (Selector) : 任何有效选择器
siblings (Selector) : 一个选择器,并且它作为第一个选择器的同辈
找到所有与表单同辈的 input 元素
HTML 代码:
<label>Name:</label>
<input name="name" />
<fieldset>
<label>Newsletter:</label>
<input name="newsletter" />
</fieldset>
</form>
<input name="none" />
jQuery 代码:
$("form ~ input")
[ <input name="none" /> ] 简单
:first:first
匹配找到的第一个元素
--------------------------------------------------------------------------------
Matches the first selected element.
Element
查找表格的第一行
HTML 代码:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:first")
[ <tr><td>Header 1</td></tr> ] :last:last
匹配找到的最后一个元素
--------------------------------------------------------------------------------
Matches the last selected element.
Element
查找表格的最后一行
HTML 代码:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:last")
[ <tr><td>Value 2</td></tr> ] :not(selector):not(selector)
去除所有与给定选择器匹配的元素
--------------------------------------------------------------------------------
Removes all
elements
matching the given selector.
Array<Element>
selector (Selector) : 用于筛选的选择器
查找所有未选中的 input 元素
HTML 代码:
<input name="apple" />
<input name="flower" checked="checked" />
jQuery 代码:
$("input:not(:checked)")
[ <input name="apple" /> ] :even:even
匹配所有索引值为偶数的元素,从 0 开始计数
--------------------------------------------------------------------------------
Matches even
elements
, zero-indexed.
Array<Element>
查找表格的1、3、5...行(即索引值0、2、4...)
HTML 代码:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:even")
[ <tr><td>Header 1</td></tr>, <tr><td>Value 2</td></tr> ] :odd:odd
匹配所有索引值为奇数的元素,从 0 开始计数
--------------------------------------------------------------------------------
Matches odd
elements
, zero-indexed.
Array<Element>
查找表格的2、4、6行(即索引值1、3、5...)
HTML 代码:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:odd")
[ <tr><td>Value 1</td></tr> ] :eq(index):eq(index)
匹配一个给定索引值的元素
--------------------------------------------------------------------------------
Matches a single element by its index.
Element
index (Number) : 从 0 开始计数
查找第二行
HTML 代码:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:eq(1)")
[ <tr><td>Value 1</td></tr> ] :gt(index):gt(index)
匹配所有大于给定索引值的元素
--------------------------------------------------------------------------------
Matches all
elements
with an index above the given one.
Array<Element>
index (Number) : 从 0 开始计数
查找第二第三行,即索引值是1和2,也就是比0大
HTML 代码:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:gt(0)")
[ <tr><td>Value 1</td></tr>, <tr><td>Value 2</td></tr> ] :lt(index):lt(index)
匹配所有小于给定索引值的元素
--------------------------------------------------------------------------------
Matches all
elements
with an index below the given one.
Array<Element>
index (Number) : 从 0 开始计数
查找第一第二行,即索引值是0和1,也就是比2小
HTML 代码:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:lt(2)")
[ <tr><td>Header 1</td></tr>, <tr><td>Value 1</td></tr> ] :header:header
匹配如 h1, h2, h3之类的标题元素
--------------------------------------------------------------------------------
Matches all
elements
that are headers, like h1, h2, h3 and so on.
Array<Element>
给页面内所有标题加上背景色
HTML 代码:
<h1>Header 1</h1>
<p>Contents 1</p>
<h2>Header 2</h2>
<p>Contents 2</p>
jQuery 代码:
$(":header").css("background", "#EEE");
[ <h1 style="background:#EEE;">Header 1</h1>, <h2 style="background:#EEE;">Header 2</h2> ] :animated:animated
匹配所有没有在执行动画效果中的元素
--------------------------------------------------------------------------------
Matches all
elements
that are currently being animated.
Array<Element>
只有对不在执行动画效果的元素执行一个动画特效
HTML 代码:
<button id="run">Run</button><div></div>
jQuery 代码:
$("#run").click(function(){
$("div:not(:animated)").animate({ left: "+20" }, 1000);
}); 内容
:contains(text):contains(text)
匹配包含给定文本的元素
--------------------------------------------------------------------------------
Matches
elements
which contain the given text.
Array<Element>
text (String) : 一个用以查找的字符串
查找所有包含 "John" 的 div 元素
HTML 代码:
<div>John Resig</div>
<div>George Martin</div>
<div>Malcom John Sinclair</div>
<div>J. Ohn
jQuery 代码:
$("div:contains('John')")
[ <div>John Resig</div>, <div>Malcom John Sinclair</div> ] :empty:empty
匹配所有不包含子元素或者文本的空元素
--------------------------------------------------------------------------------
Matches all
elements
that are empty, be it
elements
or text.
Array<Element>
查找所有不包含子元素或者文本的空元素
HTML 代码:
<table>
<tr><td>Value 1</td><td></td></tr>
<tr><td>Value 2</td><td></td></tr>
</table>
jQuery 代码:
$("td:empty")
[ <td></td>, <td></td> ] :has(selector):has(selector)
匹配含有选择器所匹配的元素的元素
--------------------------------------------------------------------------------
Matches
elements
which contain at least one element that matches the specified selector.
Array<Element>
selector (Selector) : 一个用于筛选的选择器
给所有包含 p 元素的 div 元素添加一个 text 类
HTML 代码:
<div><p>Hello</p></div>
<div>Hello again!</div>
jQuery 代码:
$("div:has(p)").addClass("test");
[ <div class="test"><p>Hello</p></div> ] :parent:parent
匹配含有子元素或者文本的元素
--------------------------------------------------------------------------------
Matches all
elements
that are parents - they have child
elements
, including text.
Array<Element>
查找所有含有子元素或者文本的 td 元素
HTML 代码:
<table>
<tr><td>Value 1</td><td></td></tr>
<tr><td>Value 2</td><td></td></tr>
</table>
jQuery 代码:
$("td:parent")
[ <td>Value 1</td>, <td>Value 1</td> ] 可见性
:hidden:hidden
匹配所有的不可见元素,input 元素的 type 属性为 "hidden" 的话也会被匹配到
--------------------------------------------------------------------------------
Matches all
elements
that are hidden, or input
elements
of type "hidden".
Array<Element>
查找所有不可见的 tr 元素
HTML 代码:
<table>
<tr style=""><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:hidden")
[ <tr style=""><td>Value 1</td></tr> ] :visible:visible
匹配所有的可见元素
--------------------------------------------------------------------------------
Matches all
elements
that are visible.
Array<Element>
查找所有可见的 tr 元素
HTML 代码:
<table>
<tr style=""><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:visible")
[ <tr><td>Value 2</td></tr> ] 属性
[attribute][attribute]
匹配包含给定属性的元素
--------------------------------------------------------------------------------
Matches
elements
that have the specified attribute.
Array<Element>
attribute (String) : 属性名
查找所有含有 id 属性的 div 元素
HTML 代码:
<p>Hello!</p>
<div id="test2"></div>
jQuery 代码:
$("div[id]")
[ <div id="test2"></div> ] [attribute=value][attribute=value]
匹配给定的属性是某个特定值的元素
--------------------------------------------------------------------------------
Matches
elements
that have the specified attribute with a certain value.
Array<Element>
attribute (String) : 属性名
value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。
查找所有 name 属性是 newsletter 的 input 元素
HTML 代码:
'<input type="checkbox" name="newsletter" value="Hot Fuzz" />
<input type="checkbox" name="newsletter" value="Cold Fusion" />
<input type="checkbox" name="accept" value="Evil Plans" />
jQuery 代码:
$("input[name='newsletter']").attr("checked", true);
[ <input type="checkbox" name="newsletter" value="Hot Fuzz" checked="true" />, <input type="checkbox" name="newsletter" value="Cold Fusion" checked="true" /> ] [attribute!=value][attribute!=value]
匹配给定的属性是不包含某个特定值的元素
--------------------------------------------------------------------------------
Matches
elements
that don't have the specified attribute with a certain value.
Array<Element>
attribute (String) : 属性名
value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。
查找所有 name 属性不是 newsletter 的 input 元素
HTML 代码:
'<input type="checkbox" name="newsletter" value="Hot Fuzz" />
<input type="checkbox" name="newsletter" value="Cold Fusion" />
<input type="checkbox" name="accept" value="Evil Plans" />
jQuery 代码:
$("input[name!='newsletter']").attr("checked", true);
[ <input type="checkbox" name="accept" value="Evil Plans" checked="true" /> ] [attribute^=value][attribute^=value]
匹配给定的属性是以某些值开始的元素
--------------------------------------------------------------------------------
Matches
elements
that have the specified attribute and it starts with a certain value.
Array<Element>
attribute (String) : 属性名
value ( String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。
查找所有 name 以 'news' 开始的 input 元素
HTML 代码:
<input name="newsletter" />
<input name="milkman" />
<input name="newsboy" />
jQuery 代码:
$("input[name^='news']")
[ <input name="newsletter" />, <input name="newsboy" /> ] [attribute$=value][attribute$=value]
匹配给定的属性是以某些值结尾的元素
--------------------------------------------------------------------------------
Matches
elements
that have the specified attribute and it ends with a certain value.
Array<Element>
attribute (String) : 属性名
value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。
查找所有 name 以 'letter' 结尾的 input 元素
HTML 代码:
<input name="newsletter" />
<input name="milkman" />
<input name="jobletter" />
jQuery 代码:
$("input[name$='letter']")
[ <input name="newsletter" />, <input name="jobletter" /> ] [attribute*=value][attribute*=value]
匹配给定的属性是以包含某些值的元素
--------------------------------------------------------------------------------
Matches
elements
that have the specified attribute and it contains a certain value.
Array<Element>
attribute (String) : 属性名
value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。
查找所有 name 包含 'man' 的 input 元素
HTML 代码:
<input name="man-news" />
<input name="milkman" />
<input name="letterman2" />
<input name="newmilk" />
jQuery 代码:
$("input[name*='man']")
[ <input name="man-news" />, <input name="milkman" />, <input name="letterman2" /> ] [selector1][selector2][selectorN][selector1][selector2][selectorN]
复合属性选择器,需要同时满足多个条件时使用。
--------------------------------------------------------------------------------
Matches
elements
that have the specified attribute and it contains a certain value.
Array<Element>
selector1 (Selector) : 属性选择器
selector2 (Selector) : 另一个属性选择器,用以进一步缩小范围
selectorN (Selector) : 任意多个属性选择器
找到所有含有 id 属性,并且它的 name 属性是以 man 结尾的
HTML 代码:
<input id="man-news" name="man-news" />
<input name="milkman" />
<input id="letterman" name="new-letterman" />
<input name="newmilk" />
jQuery 代码:
$("input[id][name$='man']")
[ <input id="letterman" name="new-letterman" /> ] 子元素
: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<Element>
index (Number) : 要匹配元素的序号,从1开始
在每个 ul 查找第 2 个li
HTML 代码:
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
jQuery 代码:
$("ul li:nth-child(2)")
[ <li>Karl</li>, <li>Tane</li> ] :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<Element>
在每个 ul 中查找第一个 li
HTML 代码:
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
jQuery 代码:
$("ul li:first-child")
[ <li>John</li>, <li>Glen</li> ] :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<Element>
在每个 ul 中查找最后一个 li
HTML 代码:
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
jQuery 代码:
$("ul li:last-child")
[ <li>Brandon</li>, <li>Ralph</li> ] :only-child:only-child
如果某个元素是父元素中唯一的子元素,那将会被匹配
如果父元素中含有其他元素,那将不会被匹配。
--------------------------------------------------------------------------------
Matches the only child of its parent.
If the parent has other child
elements
, nothing is matched.
Array<Element>
在 ul 中查找是唯一子元素的 li
HTML 代码:
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
<li>Glen</li>
jQuery 代码:
$("ul li:only-child")
[ <li>Glen</li> ] 表单
:input:input
匹配所有 input, textarea, select 和 button 元素
--------------------------------------------------------------------------------
Matches all input, textarea, select and button
elements
.
Array<Element>
查找所有的input元素
HTML 代码:
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代码:
$(":input")
[ <input type="text" />, <input type="checkbox" />, <input type="radio" />, <input type="image" />, <input type="file" />, <input type="submit" />, <input type="reset" />, <input type="password" />, <input type="button" /> ] :text:text
匹配所有的单行文本框
--------------------------------------------------------------------------------
Matches all input
elements
of type text.
Array<Element>
查找所有文本框
HTML 代码:
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代码:
$(":text")
[ <input type="text" /> ] :password:password
匹配所有密码框
--------------------------------------------------------------------------------
Matches all input
elements
of type password.
Array<Element>
查找所有密码框
HTML 代码:
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代码:
$(":password")
[ <input type="password" /> ] :radio:radio
匹配所有单选按钮
--------------------------------------------------------------------------------
Matches all input
elements
of type radio.
Array<Element>
查找所有单选按钮
HTML 代码:
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代码:
$(":radio")
[ <input type="radio" /> ] :checkbox:checkbox
匹配所有复选框
--------------------------------------------------------------------------------
Matches all input
elements
of type checkbox.
Array<Element>
查找所有复选框
HTML 代码:
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代码:
$(":checkbox")
[ <input type="checkbox" /> ] :submit:submit
匹配所有提交按钮
--------------------------------------------------------------------------------
Matches all input
elements
of type submit.
Array<Element>
查找所有提交按钮
HTML 代码:
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代码:
$(":submit")
[ <input type="submit" /> ] :image:image
匹配所有图像域
--------------------------------------------------------------------------------
Matches all input
elements
of type image.
Array<Element>
匹配所有图像域
HTML 代码:
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代码:
$(":image")
[ <input type="image" /> ] :reset:reset
匹配所有重置按钮
--------------------------------------------------------------------------------
Matches all input
elements
of type reset.
Array<Element>
查找所有重置按钮
HTML 代码:
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代码:
$(":reset")
[ <input type="reset" /> ] :button:button
匹配所有按钮
--------------------------------------------------------------------------------
Matches all input
elements
of type button.
Array<Element>
查找所有按钮.
HTML 代码:
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代码:
$(":button")
[ <input type="button" />,<button></button> ] :file:file
匹配所有文件域
--------------------------------------------------------------------------------
Matches all input
elements
of type file.
Array<Element>
查找所有文件域
HTML 代码:
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代码:
$(":file")
[ <input type="file" /> ] :hidden:hidden
匹配所有不可见元素,或者type为hidden的元素
--------------------------------------------------------------------------------
Matches all
elements
that are hidden, or input
elements
of type "hidden".
Array<Element>
查找隐藏的 tr
HTML 代码:
<table>
<tr style=""><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:hidden")
[ <tr style=""><td>Value 1</td></tr> ]
--------------------------------------------------------------------------------
匹配type为hidden的元素
HTML 代码:
<input type="text" name="email" />
<input type="hidden" name="id" />
</form>
jQuery 代码:
$("input:hidden")
[ <input type="hidden" name="id" /> ] 表单对象属性
:enabled:enabled
匹配所有可用元素
--------------------------------------------------------------------------------
Matches all
elements
that are enabled.
Array<Element>
查找所有可用的input元素
HTML 代码:
<input name="email" disabled="disabled" />
<input name="id" />
</form>
jQuery 代码:
$("input:enabled")
[ <input name="id" /> ] :disabled:disabled
匹配所有不可用元素
--------------------------------------------------------------------------------
Matches all
elements
that are disabled.
Array<Element>
查找所有不可用的input元素
HTML 代码:
<input name="email" disabled="disabled" />
<input name="id" />
</form>
jQuery 代码:
$("input:disabled")
[ <input name="email" disabled="disabled" /> ] :checked:checked
匹配所有选中的复选框元素
--------------------------------------------------------------------------------
Matches all
elements
that are checked.
Array<Element>
查找所有选中的复选框元素
HTML 代码:
<input type="checkbox" name="newsletter" checked="checked" value="Daily" />
<input type="checkbox" name="newsletter" value="Weekly" />
<input type="checkbox" name="newsletter" checked="checked" value="Monthly" />
</form>
jQuery 代码:
$("input:checked")
[ <input type="checkbox" name="newsletter" checked="checked" value="Daily" />, <input type="checkbox" name="newsletter" checked="checked" value="Monthly" /> ] :selected:selected
匹配所有选中的选项元素
--------------------------------------------------------------------------------
Matches all
elements
that are selected.
Array<Element>
查找所有选中的选项元素
HTML 代码:
<select>
<option value="1">Flowers</option>
<option value="2" selected="selected">Gardens</option>
<option value="3">Trees</option>
</select>
jQuery 代码:
$("select option:selected")
[ <option value="2" selected="selected">Gardens</option> ]
--------------------------------------------------------------------------------
Finds all option
elements
that are selected.
HTML 代码:
<select multiple="multiple">
<option value="1">Flowers</option>
<option value="2" selected="selected">Gardens</option>
<option value="3" selected="selected">Trees</option>
</select>
jQuery 代码:
$("select option:selected")
[ <option value="2" selected="selected">Gardens</option>, <option value="3" selected="selected">Trees</option> ] 属性属性
attr(name)attr(name)
取得第一个匹配元素的属性值。通过这个方法可以方便地从第一个匹配元素中获取一个属性的值。如果元素没有相应属性,则
返回
undefined 。
--------------------------------------------------------------------------------
Access a property on the first matched element. This method makes it easy to retrieve a property value from the first matched element. If the element does not have an attribute with such a name, undefined is returned.
Object
name (String) : 属性名称
返回
文档中第一个图像的src属性值。
HTML 代码:
<img src="test.jpg"/>
jQuery 代码:
$("img").attr("src");
test.jpg attr(properties)attr(properties)
将一个“名/值”形式的对象设置为所有匹配元素的属性。
这是一种在所有匹配元素中批量设置很多属性的最佳方式。 注意,如果你要设置对象的class属性,你必须使用'className' 作为属性名。或者你可以直接使用.addClass( class ) 和 .removeClass( class ).
--------------------------------------------------------------------------------
Set a key/value object as properties to all matched
elements
.
This serves as the best way to set a large number of properties on all matched
elements
. Note that you must use 'className' as key if you want to set the class-Attribute. Or use .addClass( class ) or .removeClass( class ).
jQuery
properties (Map) : 作为属性的“名/值对”对象
为所有图像设置src和alt属性。
HTML 代码:
jQuery 代码:
$("img").attr({ src: "test.jpg", alt: "Test Image" });
[ <img src= "test.jpg" alt:="Test Image" /> ]
--------------------------------------------------------------------------------
attr(key,value)attr(key,value)
为所有匹配的元素设置一个属性值。
--------------------------------------------------------------------------------
Set a single property to a value, on all matched
elements
.
jQuery
key (String) : 属性名称
value (Object) : 属性值
为所有图像设置src属性。
HTML 代码:
jQuery 代码:
$("img").attr("src","test.jpg");
[ <img src= "test.jpg" /> , <img src= "test.jpg" /> ] attr(key,fn)attr(key,fn)
为所有匹配的元素设置一个计算的属性值。
不提供值,而是提供一个函数,由这个函数计算的值作为属性值。
--------------------------------------------------------------------------------
Set a single property to a computed value, on all matched
elements
.
Instead of supplying a string value as described 'above', a function is provided that computes the value.
jQuery
key (String) : 属性名称
fn (Function) :
返回
值的函数 范围:当前元素, 参数: 当前元素的索引值
把src属性的值设置为title属性的值。
HTML 代码:
<img src="test.jpg"/>
jQuery 代码:
$("img").attr("title", function() { return this.src });
<img src="test.jpg" title="test.jpg" /> removeAttr(name)removeAttr(name)
从每一个匹配的元素中删除一个属性
--------------------------------------------------------------------------------
Remove an attribute from each of the matched
elements
.
jQuery
name (String) : 要删除的属性名
将文档中图像的src属性删除
HTML 代码:
<img src="test.jpg"/>
jQuery 代码:
$("img").removeAttr("src");
[ <img /> ] 类
addClass(class)addClass(class)
为每个匹配的元素添加指定的类名。
--------------------------------------------------------------------------------
Adds the specified class(es) to each of the set of matched
elements
.
jQuery
class (String) : 一个或多个要添加到元素中的CSS类名,请用空格分开
为匹配的元素加上 'selected' 类
HTML 代码:
<p>Hello</p>
jQuery 代码:
$("p").addClass("selected");
[ <p class="selected">Hello</p> ]
--------------------------------------------------------------------------------
为匹配的元素加上 selected highlight 类
HTML 代码:
<p>Hello</p>
jQuery 代码:
$("p").addClass("selected highlight");
[ <p class="selected highlight">Hello</p> ] removeClass(class)removeClass(class)
从所有匹配的元素中删除全部或者指定的类。
--------------------------------------------------------------------------------
Removes all or the specified class(es) from the set of matched
elements
.
jQuery
class (String) : (可选) 一个或多个要删除的CSS类名,请用空格分开
从匹配的元素中删除 'selected' 类
HTML 代码:
<p class="selected first">Hello</p>
jQuery 代码:
$("p").removeClass("selected");
[ <p>Hello</p> ]
--------------------------------------------------------------------------------
删除匹配元素的所有类
HTML 代码:
<p class="selected first">Hello</p>
jQuery 代码:
$("p").removeClass();
[ <p>Hello</p> ] toggleClass(class)toggleClass(class)
如果存在(不存在)就删除(添加)一个类。
--------------------------------------------------------------------------------
Adds the specified class if it is not present, removes the specified class if it is present.
jQuery
class (String) :CSS类名
为匹配的元素切换 'selected' 类
HTML 代码:
<p>Hello</p><p class="selected">Hello Again</p>
jQuery 代码:
$("p").toggleClass("selected");
[ <p class="selected">Hello</p>, <p>Hello Again</p> ] Html代码
html()html()
取得第一个匹配元素的html内容。这个函数不能用于
XML
文档。但可以用于XHTML文档。
--------------------------------------------------------------------------------
Get the html contents of the first matched element. This property is not available on
XML
documents (although it will work for XHTML documents).
String
HTML 代码:
<div><p>Hello</p></div>
jQuery 代码:
$("div").html();
Hello html(val)html(val)
设置每一个匹配元素的html内容。这个函数不能用于
XML
文档。但可以用于XHTML文档。
--------------------------------------------------------------------------------
Set the html contents of every matched element. This property is not available on
XML
documents (although it will work for XHTML documents).
jQuery
val (String) : 用于设定HTML内容的值
HTML 代码:
<div></div>
jQuery 代码:
$("div").html("<p>Hello Again</p>");
[ <div><p>Hello Again</p></div> ] 文本
text()text()
取得所有匹配元素的内容。
结果是由所有匹配元素包含的文本内容组合起来的文本。这个方法对HTML和
XML
文档都有效。
--------------------------------------------------------------------------------
Get the text contents of all matched
elements
.
The result is a string that contains the combined text contents of all matched
elements
. This method works on both HTML and
XML
documents.
String
HTML 代码:
<p><b>Test</b> Paragraph.</p><p>Paraparagraph</p>
jQuery 代码:
$("p").text();
Test Paragraph.Paraparagraph text(val)text(val)
设置所有匹配元素的文本内容
与 html() 类似, 但将编码 HTML (将 "<" 和 ">" 替换成相应的HTML实体).
--------------------------------------------------------------------------------
Set the text contents of all matched
elements
.
Similar to html(), but escapes HTML (replace "<" and ">" with their HTML entities).
jQuery
val (String) : 用于设置元素内容的文本
HTML 代码:
<p>Test Paragraph.</p>
jQuery 代码:
$("p").text("<b>Some</b> new text.");
[ <p><b>Some</b> new text.</p> ] 值
val()val()
获得第一个匹配元素的当前值。
在 jQuery 1.2 中,可以
返回
任意元素的值了。包括select。如果多选,将
返回
一个数组,其包含所选的值。
--------------------------------------------------------------------------------
Get the content of the value attribute of the first matched element.
In jQuery 1.2, a value is now returned for all
elements
, including selects. For multiple selects an array of values is returned.
String,Array
获得单个select的值和多选select的值。
HTML 代码:
<p></p><br/>
<select id="single">
<option>Single</option>
<option>Single2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">Multiple</option>
<option>Multiple2</option>
<option selected="selected">Multiple3</option>
</select>
jQuery 代码:
$("p").append(
"<b>Single:</b> " + $("#single").val() +
" <b>Multiple:</b> " + $("#multiple").val().join(", ")
[ <p><b>Single:</b>Single<b>Multiple:</b>Multiple, Multiple3</p>]
--------------------------------------------------------------------------------
获取文本框中的值
HTML 代码:
<input type="text" value="some text"/>
jQuery 代码:
$("input").val();
some text val(val)val(val)
设置每一个匹配元素的值。
在 jQuery 1.2, 这也可以为select元件赋值
--------------------------------------------------------------------------------
Set the value attribute of every matched element.
In jQuery 1.2, this is also able to set the value of select
elements
, but selecting the appropriate options.
jQuery
val (String) : 要设置的值。
设定文本框的值
HTML 代码:
<input type="text"/>
jQuery 代码:
$("input").val("hello world!"); val(val)val(val)
check,select,radio等都能使用为之赋值
jQuery
val (Array<String>) : 用于 check/select 的值
设定一个select和一个多选的select的值
HTML 代码:
<select id="single">
<option>Single</option>
<option>Single2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">Multiple</option>
<option>Multiple2</option>
<option selected="selected">Multiple3</option>
</select><br/>
<input type="checkbox" value="check1"/> check1
<input type="checkbox" value="check2"/> check2
<input type="radio" value="radio1"/> radio1
<input type="radio" value="radio2"/> radio2
jQuery 代码:
$("#single").val("Single2");
$("#multiple").val(["Multiple2", "Multiple3"]);
$("input").val(["check2", "radio1"]);
eq(index)eq(index)
获取第N个元素
这个元素的位置是从0算起。
--------------------------------------------------------------------------------
Reduce the set of matched
elements
to a single element.
The position of the element in the set of matched
elements
starts at 0 and goes to length - 1.
jQuery
index (Integer) :元素在jQuery对象中的索引
获取匹配的第二个元素
HTML 代码:
<p> This is just a test.</p> <p> So is this</p>
jQuery 代码:
$("p").eq(1)
[ <p> So is this</p> ] hasClass(class)hasClass(class)
检查当前的元素是否含有某个特定的类,如果有,则
返回
true。
这其实就是 is("." + class)。
--------------------------------------------------------------------------------
Checks the current selection against a class and returns true, if at least one element of the selection has the given class.
This is an alternative to is("." + class).
Boolean
class (String) : 用于匹配的类名
给包含有某个类的元素进行一个动画。
HTML 代码:
<div class="protected"></div><div></div>
jQuery 代码:
$("div").click(function(){
if ( $(this).hasClass("protected") )
$(this)
.animate({ left: -10 })
.animate({ left: 10 })
.animate({ left: -10 })
.animate({ left: 10 })
.animate({ left: 0 });
}); filter(expr)filter(expr)
筛选出与指定表达式匹配的元素集合。
这个方法用于缩小匹配的范围。用逗号分隔多个表达式
--------------------------------------------------------------------------------
Removes all
elements
from the set of matched
elements
that do not match the specified expression(s).
This method is used to narrow down the results of a search. Provide a comma-separated list of expressions to apply multiple filters at once.
jQuery
expr (Expression) : 表达式
保留带有select类的元素
HTML 代码:
<p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
jQuery 代码:
$("p").filter(".selected")
[ <p class="selected">And Again</p> ]
--------------------------------------------------------------------------------
保留第一个以及带有select类的元素
HTML 代码:
<p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
jQuery 代码:
$("p").filter(".selected, :first")
[ <p>Hello</p>, <p class="selected">And Again</p> ] filter(fn)filter(fn)
筛选出与指定函数
返回
值匹配的元素集合
这个函数内部将对每个对象计算一次 (正如 '$.each'). 如果调用的函数
返回
false则这个元素被删除,否则就会保留。
--------------------------------------------------------------------------------
Removes all
elements
from the set of matched
elements
that does not match the specified function.
The function is called with a context equal to the current element (just like '$.each'). If the function returns false, then the element is removed - anything else and the element is kept.
jQuery
fn (Function) : 传递进filter的函数
保留子元素中不含有ol的元素。
HTML 代码:
<p><ol><li>Hello</li></ol></p><p>How are you?</p>
jQuery 代码:
$("p").filter(function(index) {
return $("ol", this).length == 0;
[ <p>How are you?</p> ] is(expr)is(expr)
用一个表达式来检查当前选择的元素集合,如果其中至少有一个元素符合这个给定的表达式就
返回
true。
如果没有元素符合,或者表达式无效,都
返回
'false'. 'filter' 内部实际也是在调用这个函数,所以,filter()函数原有的规则在这里也适用。
--------------------------------------------------------------------------------
Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression.
If no element fits, or the expression is not valid, then the
response
will be 'false'. 'filter' is used internally, therefore all rules that apply there apply here, as well.
Boolean
expr (String) :用于筛选的表达式
由于input元素的父元素是一个表单元素,所以
返回
true。
HTML 代码:
<form><input type="checkbox" /></form>
jQuery 代码:
$("input[type='checkbox']").parent().is("form")
true map(callback)map(callback)
将一组元素转换成其他数组(不论是否是元素数组)
你可以用这个函数来建立一个列表,不论是值、属性还是CSS样式,或者其他特别形式。这都可以用'$.map()'来方便的建立。
--------------------------------------------------------------------------------
Translate a set of
elements
into another set of values (which may, or may not, be
elements
).
You could use this to build lists of values, attributes, css values - or even perform special, custom, selector transformations. This is provided as a convenience method for using '$.map()'.
jQuery
callback (Function) : 给每个元素执行的函数
把form中的每
1、
AJAX
初步认识
AJAX
的全称AsynchronousJavaScriptAnd
XML
.
AJAX
2005年由Google发起并流行起来的编程方法,
AJAX
不一个新的编程语言,但它一个使用已有标准的新的编程技术。一种创建交互式网页应用的网页发技术。 使用
AJAX
可以创建更好,更快,更用户界面友好的Web应用。
AJAX
技术基于Javascript和HTTPRequest.
Ajax
包含下列技术: 基于web标准(standards-basedpresentation)XHTML+CSS的表示; 使用DOM(DocumentObjectModel)进行动态显示及交互; 使用
XML
和XSLT进行数据交换及相关操作; 使用
XMLHttpRequest
进行异步数据查询、检索; 使用JavaScript将所有的东西绑定在。 2、使用
Ajax
的好处 1、通过异步模式,提升了用户体验 2、优化了浏览器和器之间的传输,减少不必要的数据往返,减少了带宽占用 3、
Ajax
引擎在客户端运行,承担了一部分本来由器承担的工作,从而减少了用户量下的器负载。 3、
AJAX
的特
Ajax
可以实现动态不刷新(局部刷新) 就能在不更新整个页面的前提下维护数据。这使得Web应用程序更为迅捷地回应用户动作,并避免了在网络上那些没有改变过的信息。 4、
XMLhttprequest
对象。
Ajax
的核心JavaScript对象
XmlHttpRequest
。该对象在InternetExplorer5中首次引入,它一种支持异步请求的技术。简而言之,
XmlHttpRequest
使您可以使用JavaScript向器提出请求并处理响应,而不阻塞用户。通过
XMLHttpRequest
对象,Web发人员可以在页面加载以后进行页面的局部更新。 5、
AJAX
技术体系的组成部分有哪些? HTML,css,dom,
xml
,
xmlHttpRequest
,javascript 6、
AJAX
应用和传统Web应用有什么不同。 在传统的Javascript编程中,如果想得到器端数据库或文件上的信息,或者客户端信息到器,需要建立一个HTMLform然后GET或者POST数据到器端。用户需要击"Submit"按钮来或者接受数据信息,然后等待器响应请求,页面重新加载。 因为器每次都会
返回
一个新的页面,所以传统的web应用有可能很慢而且用户交互不友好。 PHP培训教程之
AJAX
技术全文共3页,当前为第1页。使用
AJAX
技术,就可以使Javascript通过
XMLHttpRequest
对象直接与器进行交互。 PHP培训教程之
AJAX
技术全文共3页,当前为第1页。 通过HTTPRequest,一个web页面可以一个请求到web器并且接受web器
返回
的信息(不用重新加载页面),展示给用户的还通一个页面,用户感觉页面刷新,也看不到到Javascript后台进行的请求和接受响应。 7、
AJAX
请求总共有多少种CALLBACK
Ajax
请求总共有八种Callback onSuccess onFailure onUninitialized onLoading onLoaded onInteractive onComplete onException 8.
Ajax
和javascript的区别 javascript一种在浏览器端执行的脚本语言,
Ajax
一种创建交互式网页应用的发技术,它利用了一系列相关的技术其中就包括javascript。 Javascript由网景发的一种脚本语言,它和sun的java语言没有任何关系的,它们相似的名称只一种行销策略。 在一般的web发中,javascript在浏览器端执行的,们可以用javascript控制浏览器的行为和内容。 9、在
Ajax
应用中信息如何在浏览器和器之间传递的 通过
XML
数据或者字符串 10、在浏览器端如何得到器端响应的
XML
数据。
XMLHttpRequest
对象的
response
XMl
属性 12、
XMLHttpRequest
对象在IE和Firefox中创建的不同 有,IE中通过newActiveXObject()得到,Firefox中通过new
XMLHttpRequest
()得到10、介绍一下
XMLHttpRequest
对象的常用方法和属性。 open("method","URL")建立对器的调用,个参数HTTP请求可以为GET,POST或任何器所支持的您想调用的。 第二个参数请求页面的URL。 send()方法,具体请求 abort()方法,停止当前请求! readyState属性请求的状态有5个可取值0=未初始化,1=正在加载,2=以加载,3=交互中,4=完成
response
Text属性器的响应,表示为一个串 reponse
XML
属性器的响应,表示为
XML
OleObject lole_tenyear
lole_tenyear = CREATE oleobject
lole_tenyear.ConnectToNewObject("Microsoft.
XML
Http") //
//lole_tenyear.ConnectToNewObject('WinHttp.WinHttpRequest.5.1')
lole_tenyear.open ("POST",ls_url, false)
lole_tenyear.setRequestHeader ("Content-type", "application/x-www-form-urlencoded")
lole_tenyear.setRequestHeader('Content-Length',string(len(ls_data)))
在网站建设过程中,
XML
可以将数据从 HTML 文档中分离出来,所谓分离,就是在 HTML 中仅仅编写与页面相关的程序,而网站中的数据则储存在
XML
文档中,这种方法把程序和数据分离开来,使得文档更为清晰。当 HTML 需要调用
XML
中的数据时,通过 JAVASCRIPT 或者 JQUERY 语言实现。
编码完成后,页面上没有显示数据,增加语句 alert (
XML
Ht...
方法: open与send
open(method,url, asynch) :建立对服务器的调用
(method 表示请求方式, url 请求路径, asynch 是否异步,对于 asynch 的值通常不会设置, 默认是true,表示异步发送请求)
send(content) :向服务器发送请求
onreadystatechange :状态回调函数
response
Text/
response
XML
:服务器的响应字符串
status:服务器
返回
的HTTP状态码
statusText: 服务器
返回
的HTTP状态信息
readyState :对象状态(0-4)
0 = 未初始化 1 = 正在加载 2 = 已加载
3 = 交互中
4 = 完成
AJAX
大家已经都知道了,是为了实现异步通讯,提高用户体验度,而将很多旧知识(
XML
,DOM,JavaScript,HTML,Jquery,Css……)重新融合的一个新的知识框架。而,
XMLHttpRequest
对象则是其中的重重之中。这篇博客重点总结一下这个对象的使用。当然还是按照经典的五步法来学习,以后在实践中有更多更好的想法,会拿出来分享的!
首先,需要先了解
2、
XMLHttpRequest
的方法和属性
XMLHttpRequest
包含了基本的属性和方法,正是通过这些方法和属性实现了对服务器的通讯。
Ajax
依赖
XMLHttpRequest
完成与服务器的通信。
XMLHttpRequest
的基本方法如下:
Abort():停止发送当前请求。
getAll
Response
Header
var my
XmlHttpRequest
;
function doGetmess(){
var tx= document.getElementById('showText');
tx.innerHTML="你好啊"; checkNameAndPsw();}
function get
Xml
httpObject(){ if(window.
XMLHttpRequest
){
1. onreadystatechange:当readyState属性发生变化时调用的函数。
2. readyState:表示请求的状态,有5种状态:(未初始化)、1(正在加载)、2(已加载)、3(交互中)、4(完成)。
3.
response
Text:
返回
服务器响应的文本内容。
4.
response
XML
:
返回
服务器响应的
XML
文档对象。
5. status:
返回
服务器响应的状态码。
6. statusText:
返回
服务器响应的状态文本。
7. timeout:设置请求超时时间。
8. withCredentials:设置是否允许跨域请求发送cookie等凭据信息。