I have a sample code:
<a href=""></a>
<textarea></textarea>
<object></object>
<img src="" />
<div id="content"></div>
I had using this:
jQuery("a").mouseover(function() {
alert();
});
jQuery("textarea").mouseover(function() {
alert();
});
jQuery("object").mouseover(function() {
alert();
});
jQuery("img").mouseover(function() {
alert();
});
jQuery("#content").mouseover(function() {
alert();
});
Can I using this ?
var object = ["a","textarea","object","img", "content"];
jQuery.each(object, function() {
jQuery(this).mouseover(function(){
alert();
});
});
How to ideas using jQuery mouseover from an array ?
In the context of your
.each()loop,thiswill be the current element from the array, i.e., a string, except that each string will be wrapped in an object (as explained at the$.each()doco page), so you need to usethis.toString():…and then it will have the same effect as
jQuery("a"),jQuery("textarea")and so forth. Which will work as long as each string is a valid selector. Or, better, make use of the arguments that jQuery passes to your function:The final element in your array is an
id, so it needs a#:Then in the context of the mouseover handler,
thisis the element that the event occurred on, so just usejQuery(this)rather thanjQuery("#" + this). Putting that all together:Demo: http://jsfiddle.net/GBtFY/