简体   繁体   English

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

[英]How to refer to $(this) of plugin instead $(this) of each function? jQuery

Developing a little plugin, I found a question that is tearing me up inside. 开发一个小插件,我发现了一个让我内心撕裂的问题。 To call my plugin, I use: 要调用我的插件,我使用:

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

In a given moment, I need use a each inside of my plugin, following is the code: 在给定的时刻,我需要使用我的插件中的每个内部,以下是代码:

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())
})

After this piece of code I need use another "each" function, but now I don't want use the $(this) of 'each', I want use the "global this". 在这段代码之后我需要使用另一个“each”函数,但是现在我不想使用$(this)的'each',我想使用“global this”。 In other words, now I want refer to $('#input'), the element that call my plugin. 换句话说,现在我想引用$('#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))
}

How I can tell to jQuery what's the desired this? 我怎么告诉jQuery这是什么?

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

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

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