开发者

Can this be written more elegantly?

开发者 https://www.devze.com 2023-03-15 04:49 出处:网络
I was wondering if a callback would work with this simple two line jquery below? I\'m pretty new so I just am looking to get bett开发者_如何学Pythoner :)

I was wondering if a callback would work with this simple two line jquery below? I'm pretty new so I just am looking to get bett开发者_如何学Pythoner :)

Thanks!

jQuery('.entry:first').addClass('firstEntry');
jQuery('.entry:first').removeClass('big');


jQuery('.entry:first').addClass('firstEntry').removeClass('big');


jQuery('.entry:first').addClass('firstEntry').removeClass('big');


jQuery supports chaining...

jQuery('.entry:first').addClass('firstEntry').removeClass('big');

Also, note there is a .toggleClass() method.


You could do this:

jQuery('.entry:first').addClass('firstEntry').removeClass('big');


var $entries = $(".entry");
$entries.first().addClass('firstEntry').removeClass('big');

or something a bit more readable...

var $entries = $(".entry");
$entries
  .first()
    .addClass('firstEntry')
    .removeClass('big');
0

精彩评论

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