开发者

Combining string and object as a jQuery selector

I'm having a hard time finding the solution for this for some reason -- perhaps it's right under my nose.

But is there a way to essentially combine a string and an object so I'm开发者_运维知识库 not repeating the same method on a certain event?

$j(window).resize(function(){
  //stuff here
});

$j('body').resize(function(){
  //same stuff here
});

Maybe I'm just thinking about it the wrong way?

Thanks.


You can add any selector to another with .add

E.g.:

$j(window).add('body').resize(function(){
  //same stuff here
});


Assign the function to a variable.

var handler = function(){
  //stuff here
};

$j(window).resize(handler);

$j('body').resize(handler);

Or pass an Array of the DOM elements like this:

$j([window, document.body]).resize(function(){
      //stuff here
});


Try this:

$("body").add(window).resize(function(){
   //stuff here
});


I think you are talking about javascript reserverd words. You have list here.

http://www.quackit.com/javascript/javascript_reserved_words.cfm

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜