How to add functions to some jQuery objects, but not others?

前端 未结 4 988
我寻月下人不归
我寻月下人不归 2021-02-07 08:37

Let\'s say I have a

    list:

      ...

    I want to select it with jQuery, then add s

4条回答
  •  感情败类
    2021-02-07 09:06

    I think you want to add a function to that DOM Object.

    $(function(){
        // [0] gets the first object in array, which is your selected element, you can also use .get(0) in jQuery
        $("#test")[0].addProduct = function(info){
            alert("ID: " + this.id + " - Param: " + info);
        };
    
    
        $("#test")[0].addProduct("productid");
    });
    

    Above script wil alert "ID: test - Param: productid"

    A live example: http://jsfiddle.net/jJ65A/1/

    Or normal javascript

    $(function(){
        document.getElementById("test").addProduct = function(info){
            alert(info);
        };
    });
    

提交回复
热议问题