开发者

Using apply with jQuery

开发者 https://www.devze.com 2023-04-11 00:48 出处:网络
Why does this work...: $(\'.foo\').hide开发者_JS百科() ...and this doesn\'t?: $(\'.foo\').hide.apply(this,[])

Why does this work...:

$('.foo').hide开发者_JS百科()

...and this doesn't?:

$('.foo').hide.apply(this,[])

I'm trying to write a function that passes arguments into hide().


You are passing the wrong object, this should be the element you want to hide.

$.fn.hide.apply($('.foo'), []);

Working demo.


Obviously because of the wrong context. It should be:

$.fn.hide.apply($('#hlogo'), []);


consider the following code:

  var newhide = jQuery.fn.hide;
  jQuery.fn.hide = function() {
    console.log(this, arguments);
    return newhide.apply(this, arguments);
  };


jQuery hide already accepts arguments as per the doc. And, you can pass those arguments like this:

$('.foo').hide(duration, fn);

If you were making your own jQuery methods, then there's a jQuery framework already in place for jQuery methods. this will be set to the jQuery object and any arguments passed with the function will be in place.

Can you explain why you're trying to do what you're doing because it isn't making sense to me.

0

精彩评论

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