简体   繁体   English

如何从.data()中的函数获取元素的变量

[英]How to get a variable to the element from a function in .data()

var el = $('#someElement');

el.data('f', function() {
  console.log(this); // return an object to the window
});

So i would like something that return me an object like "el" from inside the function. 所以我想要从函数内部返回诸如“ el”之类的对象的东西。

The reason is that the element is going to be duplicated and i don't know on which element the function is applied. 原因是该元素将被复制,我不知道在哪个元素上应用该功能。

You could do (if i understand correctly, which i'm unsure) 你可以做(​​如果我理解正确,我不确定)

var el = $('#someElement');
var clone = el.clone();
el.data('f', function() {
  console.log(clone); // return an object to the window
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM