在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


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

查看所有标签

猜你喜欢:

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

Math Adventures with Python

Math Adventures with Python

Peter Farrell / No Starch Press / 2018-11-13 / GBP 24.99

Learn math by getting creative with code! Use the Python programming language to transform learning high school-level math topics like algebra, geometry, trigonometry, and calculus! In Math Adventu......一起来看看 《Math Adventures with Python》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

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

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具