如何找出保存数据对象的元素

时间:2011-12-27 14:22:45

标签: javascript jquery

如果我将数据对象传递给函数,如:

$("#someobject").data({
    "prp1":"x",
    "dosomething":function(){
    callthisfunction(this);  //<---- HERE the data ref is sent to a function
   }
});

...
function callthisfunction(in_data)
{
  //how is the data element?
  var theElementHoldingTheDataIs = in_data.????;  //<--- how can I get $("#someobject")


}

我的问题是:有没有办法从数据通知它依赖或属于哪个对象?

1 个答案:

答案 0 :(得分:2)

您可以使用闭包:

var obj = $("#someobject");
obj.data({
    "prp1": "x",
    "dosomething": (function(scope) {
        return function() {
            callthisfunction(scope); //<---- HERE the data ref is sent to a function
        }
    })(obj)
});

Example

<小时/> 或者,如果您只想发送数据对象:

var obj = $("#someobject");
obj.data({
    "prp1": "x",
    "dosomething": (function(scope) {
        return function() {
            callthisfunction(scope.data());
        }
    })(obj)
});