Add jQuery function to specific elements

前端 未结 9 1479
悲&欢浪女
悲&欢浪女 2020-12-07 18:56

I know that you can add new jQuery functions by $.fn.someFunction = function()

However, I want to add functions to specific elements only. I tried this

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 19:20

    The most obvious solution is to assign a function as the object's property:

    obj.prop("myFunc", function() {
      return (function(arg) {
        alert("It works! " + arg);
      });
    });
    

    Then call it on the object this way:

    obj.prop("myFunc")("Cool!");
    

    Note: your function is the return value of the outer one, see: http://api.jquery.com/prop/#prop-propertyName-function

提交回复
热议问题