jquery - 如何创建 jQuery 函数(初学者)

标签 jquery function syntax parameters

我刚刚开始使用 jQuery,并且正在尝试创建一个函数。我希望得到语法方面的帮助。

http://jsfiddle.net/e2s8besv/

<p>blink me<p>

<style>p {display:none;}</style>

<script>
    (function( $ ) {
        $.fn.blink= function(speed) {
            var speed = $(speed).val();
            $(this).fadeIn(speed).fadeOut(speed).blink(speed);
        };
    }( jQuery));

    $("p").blink(1500);
</script>      

最佳答案

您不需要在那里使用val():

$.fn.blink= function(speed) {
    $(this).fadeIn(speed).fadeOut(speed).blink(speed);
};

jsFiddle

但我建议也返回 jQuery 实例。因此,您将来将能够访问此元素或使用元素集合,而不仅仅是第一个匹配的元素:

$.fn.blink= function(speed) {
    return this.each( function() {
        $(this).fadeIn(speed).fadeOut(speed).blink(speed);
    });
};

jsFiddle

关于jquery - 如何创建 jQuery 函数(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28590822/

相关文章:

javascript - 删除功能在 ExtJS 5.1.1 中不运行

java - Java中的正则表达式: Pattern.编译( "J.*\\d[0-35-9]-\\d\\d-\\d\\d")

javascript - 过滤 jcarousel 内容

jquery,mousenter上的双slidetoggle

jquery - 将 ALT 字段的值复制到 <img> 内的 TITLE 字段中

c - 在 C 中将未初始化的二维数组作为参数传递

c++ - 从函数 c++ 返回一个列表<int>

syntax - prolog中_和_variable有什么区别?

methods - 什么类型对方法的 `self` 参数有效?

jquery - 如何在 Jquery UI 对话框中实现 "confirmation"对话框?