如何引用插件的$(this)而不是每个函数的$(this)?jQuery.



开发一个小插件,我发现了一个让我内心撕裂的问题。要调用我的插件,我使用:

$('#input').myPlugin();

在给定的时刻,我需要在我的插件中使用每个内部,以下是代码:

var list = []
var str = 'just a test <b>foo</b> blablabla <b>bar</b>'
str.children('b').each(function(i){  
    // Here I want use the $(this) of each function
    list.append($(this).text())
})

在这段代码之后,我需要使用另一个"each"函数,但现在我不想使用"each"的$(this),我想使用"global this"。换句话说,现在我想引用$('#input'),调用我的插件的元素。

hashtag.each(function(i)){ 
      // In the first "this" I want refer to $('input'), in the second, to this of each function.
      $(this).functionToFindText($(this))
}

我如何告诉jQuery这个想要什么?

.each 循环之前声明一个变量var $this = $(this),并在内部回调中引用$this

最新更新