开发者

elem as this in function

开发者 https://www.devze.com 2023-02-02 10:40 出处:网络
I have jquery function: function handleSplittingPrices() { var customerId = $(this).val(); loadSplittingPrices(customerId);

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号