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
精彩评论