Home  >  Article  >  Web Front-end  >  jquery 插件学习(三)_jquery

jquery 插件学习(三)_jquery

WBOY
WBOYOriginal
2016-05-16 17:51:05852browse

例如:

复制代码 代码如下:

$(this).test().hide().height();

要实现类似的连写行为,就应该在每个插件方法中,返回一个jquery对象,除非方法需要明确返回值。返回的jquery对象通常就是this所引用的对象。如果使用each()方法迭代this,则可以直接返回迭代的结果。针对上一节的示例,进一步修改
复制代码 代码如下:

jQuery.fn.test = function(){
return this.each(function(){ //遍历匹配的元素,此处的this表示对象集合
alert(this.nodeName); //提示当前jquery对象的dom节点名称
})
}

然后,我们就可以在应用示例中连写行为了,例如,在下面的示例中,先弹出提示节点的名称的信息,然后使用当前节点名称改写当前元素内包含的信息,最后在慢慢隐藏该元素。
复制代码 代码如下:

$('body *').click(function(){
$(this).test().html(this.nodeName).hide(1000);
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn