elem as this in function
I have jquery function:
function handleSplittingPrices() {
var customerId = $(this).val();
loadSplittingPrices(customerId);
}
I want to execute it with elem as "this":
var elem=$('...');
handleSplittingPrices() <-- here elem will be this
开发者_运维知识库
How can I do it?
You're looking for the call
method:
handleSplittingPrices.call(elem);
Note that elem
is a jQuery object, not a DOM element, so you won't need to call $
inside the function.
Use call
:
handleSplittingPrices.call(elem);
You can't. Obviously you don't know what "this" is for.
function handleSplittingPrices(elem) { var customerId = $(elem).val(); loadSplittingPrices(customerId); }
var elem=$('...'); handleSplittingPrices(elem);
Wow. Now wasn't that simpler than trying to define the whole concept of Object Oriented programming?
精彩评论