在jquery中创建函数

栏目: jQuery · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/7470521/create-function-in-jquery
我正在尝试创建一个jquery函数,并使用下面的代码,但没有得到任何输出.请告诉我制作它的过程.
<script type="text/javascript">
    (function($){
        $.fn.myplugin = function(){
            alert(this.id);
        };
    })(jQuery);

    $("#sample").myplugin();
</script>
你可以在这里看到工作代码 – http://jsfiddle.net/gryzzly/YxnEQ/

请注意,在运行上述代码时,您需要一个id为“sample”的元素才能出现在文档中.此外,这指向jQuery对象,因此为了获取元素id,您需要使用以下任一方法:

alert( this[0].id );

要么

alert( this.attr('id') );

第一个是可取的,因为它更快.

翻译自:https://stackoverflow.com/questions/7470521/create-function-in-jquery


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Distributed Systems

Distributed Systems

Sukumar Ghosh / Chapman and Hall/CRC / 2014-7-14 / USD 119.95

Distributed Systems: An Algorithmic Approach, Second Edition provides a balanced and straightforward treatment of the underlying theory and practical applications of distributed computing. As in the p......一起来看看 《Distributed Systems》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具