Javascript 如何引用插件的$(this)而不是每个函数的$(this)?jQuery
在开发一个小插件时,我发现了一个让我心碎的问题。要调用我的插件,我使用:Javascript 如何引用插件的$(this)而不是每个函数的$(this)?jQuery,javascript,jquery,Javascript,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
$('#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())
})
我如何告诉jQuery这是什么?在
之前声明一个变量var$this=$(this)
。每个循环,并在内部回调中引用$this
。我不知道这是否是最好的方法,但这是有效的,您的答案是唯一的,所以谢谢!这是最常见的方式。很好。
hashtag.each(function(i)){
// In the first "this" I want refer to $('input'), in the second, to this of each function.
$(this).functionToFindText($(this))
}