Jquery, $(this) 外部函数
Jquery, $(this) outside function
我想在 each loop
中触发一个函数。
问题来自 $(this)
,它在 之外 each loop
function look () {
$(this).css("color","blue");
};
$(".text").each(function(){
look();
// other functions()
//...
});
我知道我可以将 $(this)
放在 each loop
中,但我想继续这样做,因为我有许多小功能要放置。我能怎么做 ?谢谢。尼古拉斯.
试试这个:
function look (el) {
el.css("color","blue");
};
$(".text").each(function(){
look($(this));
});
您可以使用 look.call(this) 来更改 this 的上下文;通过这种方式,您不需要更改函数 look()。
看:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
我想在 each loop
中触发一个函数。
问题来自 $(this)
,它在 之外 each loop
function look () {
$(this).css("color","blue");
};
$(".text").each(function(){
look();
// other functions()
//...
});
我知道我可以将 $(this)
放在 each loop
中,但我想继续这样做,因为我有许多小功能要放置。我能怎么做 ?谢谢。尼古拉斯.
试试这个:
function look (el) {
el.css("color","blue");
};
$(".text").each(function(){
look($(this));
});
您可以使用 look.call(this) 来更改 this 的上下文;通过这种方式,您不需要更改函数 look()。
看:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call