【问题标题】:How to get the element that jQuery plugin is attached?如何获取附加 jQuery 插件的元素?
【发布时间】:2016-12-12 02:18:37
【问题描述】:

我正在使用一个 jquery 插件。有没有办法从插件的函数中获取插件附加的元素?

我试过this$(this)。但它们都不起作用。

$('.myelement').plugin({
    something: function(){
       //in here I want to access $('.myelement)
    } 
});

【问题讨论】:

  • 那将取决于插件,也就是这里使用的插件
  • 好吧,它没有提供这样的访问权限,所以我认为可能有一种方法可以使用 javascript power 访问它
  • 在您的具体情况下,既然您使用的是 id,为什么不使用 $('#myelement') 而不是 this
  • 另一个选项是$('selector').each(function() { var $this = $(this); $this.plugin({ something: function() { //$this } }); })
  • 你能告诉我们有问题的插件吗?

标签: javascript jquery plugins jquery-plugins


【解决方案1】:

看起来插件正在向回调方法公开该信息。如果您要处理多个元素,一种解决方法是遍历元素列表,然后使用可在回调中使用的本地引用为每个元素调用插件

$('selector').each(function() {
  var $this = $(this);
  $this.plugin({
    something: function() { //$this 
    }
  });
})

【讨论】:

    猜你喜欢
    • 2014-03-19
    • 2010-12-13
    • 2016-12-06
    • 2013-10-01
    • 1970-01-01
    • 2016-08-22
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多