开发者

Is it Ok to pass jquery objects to different functions?

开发者 https://www.devze.com 2022-12-08 07:08 出处:网络
I am doing the following : $(function() { var $txt = $(\"#text1\"); 开发者_StackOverflowcallMe($txt, $txt.val());

I am doing the following :

$(function() {
    var $txt = $("#text1");

开发者_StackOverflow    callMe($txt, $txt.val());                   


});

function callMe(t, vlue) {
    t.html(vlue)
}

I will be calling the callMe() function multiple times.

Is it Ok to do the way i have shown or is there a better way?


There's no problem with passing jQuery objects around.

It will be much more efficient than re-running the selector each time - that's for sure.


jQuery is JavaScript. And this is absolutely valid in JavaScript. You can even just pass the jQuery object an do this:

function callMe(t) {
    t.html(t.val());
}
0

精彩评论

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