随笔 - 4  文章 - 0 评论 - 0 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(3)

随笔分类(4)

随笔档案(4)

搜索

  •  

积分与排名

  • 积分 - 2788
  • 排名 - 558

最新评论

阅读排行榜

评论排行榜

$(html)

根据提供的原始HTML标记字符串,动态创建由jQuery对象包装的DOM元素。

Create DOM elements on-the-fly from the provided String of raw HTML.

返回值

jQuery

参数

  • html (String): 用于动态创建DOM元素的HTML标记字符串

示例

说明:

动态创建一个div元素(以及其中的所有内容),并将它追加到ID值为body的元素中。在这个函数的内部,是通过临时创建一个元素,并将这个元素的innerHTML属性设置为给定的标记字符串,来实现标记到DOM元素转换的。所以,这个函数既有灵活性,也有局限性。

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元素

示例

说明:

与 $("div > p") 相同。

HTML 代码:
<p>one</p> <div><p>two</p></div> <p>three</p>
jQuery 代码:
$(document).find("div > p")
结果:
[ <p>two</p> ]

说明:

把页面的背景颜色设置为黑色。

jQuery 代码:
$(document.body).background( "black" );

说明:

隐藏表单中的所有input元素。

jQuery 代码:
$( myForm.elements ).hide()

$(fn)

$(document). ready()的简写方式,允许你绑定一个在DOM文档载入完成后执行的函数。这个函数的作用如同$(document).ready()一样,只不过用 这个函数时,需要把页面中所有其他的$()操作符都包装到其中来。从技术上来说,这个函数是可链接的--但真正以这种方式链接的情况并不多。你可以在一个 页面中使用任意多个$(document).ready事件。 要详细了解ready事件,见ready(Function)。

A shorthand for $(document).ready(), allowing 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 all of the other $() operations on your page. 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

参数

  • fn (Function): 当DOM载入完成后要执行的函数

示例

说明:

当DOM就绪可用时,执行其中的函数。

jQuery 代码:
$(function(){ // DOM文档已经载入就绪 });

$(expr, context)

这 个函数接收一个包含CSS或基本的XPath选择符的字符串,然后用这个字符串去匹配一组元素。 jQuery的核心功能都是通过这个函数实现的。 jQuery中的一切都构建于这个函数之上,或者说都是在以某种方式使用这个函数。这个函数最基本的用法就是向它传递一个表达式(通常由CSS或 XPath选择符组成),然后根据这个表达式来查询所有匹配的元素。 在默认情况下,$()查询的是当前HTML文档中的DOM元素。

This function accepts a string containing a CSS or basic XPath 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 or XPath), which then finds all matching elements. By default, $() looks for DOM elements within the context of the current HTML document.

返回值

jQuery

参数

  • expr (String): 用来查询用的字符串
  • context (Element|jQuery): (可选)作为上下文的DOM元素、文档或jQuery对象。

示例

说明:

找到所有是div元素子元素的p元素。

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])

说明:

查询指定XML文档中的所有div元素。

jQuery 代码:
$("div", xml.responseXML)

$.extend(prop)

扩展jQuery对象。可以用于把函数添加到jQuery名称空间中,以及添加插件方法(插件)。

Extends the jQuery object itself. Can be used to add functions into the jQuery namespace and to add plugin methods (plugins).

返回值

Object

参数

  • prop (Object): 要合并到jQuery对象中的对象

示例

说明:

添加两个插件方法。

jQuery 代码:
1 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名称空间中添加两个函数。

jQuery 代码:
jQuery.extend({ min: function(a, b) return a < b ? a : b; }, max: function(a, b) return a > b ? a : b; } });

$.noConflict()

运 行这个函数将变量$的控制权让渡给第一个实现它的那个库。这样可以确保jQuery不会与其他库的$对象发生冲突。 在运行这个函数后,就只能使用iQuery变量访问iQuery对象。例如,在要用到$("div p")的地方,就必须换成iQuery("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").

返回值

undefined

示例

说明:

将$引用的对象映射回原始的对象,让渡变量$

jQuery 代码:
1 jQuery.noConflict();  //  开始使用jQuery jQuery("div p").hide(); // 使用其他库的 $() $("content").style.display = 'none'; 

说明:

恢复使用别名$,然后创建并执行一个函数,在这个函数的作用域中仍然将$作为jQuery的别名来使用。在这个函数中,原来的$对象是无效的。这个函数对于大多数不依赖于其他库的插件都十分有效。

jQuery 代码:
1jQuery.noConflict(); (function($) { $(function() // 使用 $ 作为 jQuery 别名的代码 }); })(jQuery); // 使用 $ 作为别名的其他库的代码

eq(pos)

将匹配的元素集合缩减为一个元素。这个元素在匹配元素集合中的位置变为0,而集合长度变成1。

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

参数

  • pos (Number): 要保留的元素的索引

示例

HTML 代码:
<p>This is just a test.</p><p>So is this</p>
jQuery 代码:
$("p").eq(1)
结果:
[ <p>So is this</p> ]

get()

取得所有匹配的(DOM)元素集合。这是取得所有匹配元素的一种向后兼容的方式(不同于jQuery对象,而实际上两者都是元素数组)。

Access all matched 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).

返回值

Array<Element>

示例

说明:

选择文档中的所有图像,并返回相应的DOM元素数组。

HTML 代码:
<img src="test1.jpg"/> <img src="test2.jpg"/>
jQuery 代码:
$("img").get();
结果:
[ <img src="test1.jpg"/> <img src="test2.jpg"/> ]

get(num)

取得其中一个匹配的元素。 num表示取得第几个匹配的元素。

Access a single matched element. num is used to access the Nth element matched.

返回值

Element

参数

  • num (Number): 取得第num个位置上的元素

示例

说明:

选择文档中所有的图像,并返回第一个。

HTML 代码:
<img src="test1.jpg"/> <img src="test2.jpg"/>
jQuery 代码:
$("img").get(0);
结果:
[ <img src="test1.jpg"/> ]

gt(pos)

将匹配的元素集合缩减为给定位置之后的所有元素。这个元素在匹配元素集合中的位置变为0,而长度变成1。

Reduce the set of matched elements to all elements after a given position. The position of the element in the set of matched elements starts at 0 and goes to length - 1.

返回值

jQuery

参数

  • pos (Number): 把集合缩减为这个位置之后的所有元素

示例

HTML 代码:
<p>This is just a test.</p><p>So is this</p>
jQuery 代码:
$("p").gt(0)
结果:
[ <p>So is this</p> ]

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"></div><b></b><span id="foo"></span>
jQuery 代码:
$("").index( $('#foobar')[0] )
结果:
0

说明:

返回ID值为foo的元素的索引值。

HTML 代码:
<div id="foobar"></div><b></b><span id="foo"></span>
jQuery 代码:
$("").index( $('#foo'))
结果:
2

说明:

因为没有ID值为bar的元素,所以返回 -1。

HTML 代码:
<div id="foobar"></div><b></b><span id="foo"></span>
jQuery 代码:
$("*").index( $('#bar'))
结果:
-1

length

当前匹配的元素数量。

The number of elements currently matched.

返回值

Number

示例

HTML 代码:
<img src="test1.jpg"/> <img src="test2.jpg"/>
jQuery 代码:
$("img").length;
结果:
2

lt(pos)

将匹配的元素集合缩减为给定位置之前的所有元素。这个元素在匹配元素集合中的位置变为0,而长度变成1。

Reduce the set of matched elements to all elements before a given position. The position of the element in the set of matched elements starts at 0 and goes to length - 1.

返回值

jQuery

参数

  • pos (Number): 把集合缩减为这个位置之下的所有元素

示例

HTML 代码:
<p>This is just a test.</p><p>So is this</p>
jQuery 代码:
$("p").lt(1)
结果:
[ <p>This is just a test.</p> ]

size()

当前匹配的元素数量。

The number of elements currently matched.

返回值

Number

示例

HTML 代码:
<img src="test1.jpg"/> <img src="test2.jpg"/>
jQuery 代码:
$("img").size();
结果:
2
posted on 2008-08-15 11:05 wuxsoft 阅读(460) 评论(0)  编辑 收藏 引用 所属分类: ajax
只有注册用户登录后才能发表评论。