4 ms·
var ims = document.getElementsByTagName('img'); var event = document.createEvent("MouseEvent"); event.initMouseEvent("mouseover", true, true, window);
by obituary_latte 14y ago
var ims = document.getElementsByTagName('img');
var event = document.createEvent("MouseEvent");
event.initMouseEvent("mouseover", true, true, window);
for(var i=0;i<ims.length; i++){
ims[i].dispatchEvent(event);
}
:(
- tlrobinson 14y ago$("img").mouseover()
- obituary_latte 14y agoWait...jQuery is available?? double :(
- redidas 14y agoWas this double :( a response to the fact that jQuery is available and shouldn't have needed to be? Or because you just wrote 6 lines of javascript when you could have written 1? I read it as the first, and had this immediate reaction: - I'd rather write the 1 line of jQuery over the 6 in vanilla javascript - I'd rather read the 1 line of jQuery to understand what is going on instead of the 6 in vanilla javascript - jQuery didn't actually impact your experience much... as you didn't realize it might even be available.
- waxjar 14y agoI'd take the 6 lines of vanilla javascript any day over loading the full jQuery library to get away with writing only a single line. If jQuery isn't use anywhere else on the page, that is.
- FuzzyDunlop 14y agowindow.setInterval(function() { $('img').mouseover(); }, 500); So you can catch the ones that haven't been created yet.
- obituary_latte 14y agoaww man... var event = document.createEvent("MouseEvent"); event.initMouseEvent("mouseover", true, true, window); function save_them_prontos(){ var ims = document.getElementsByTagName('img'); for(var i=0;i<ims.length; i++){ if(ims[i].id == ''){ ims[i].id = i; ims[i].dispatchEvent(event); } } time_dem(); } function time_dem(){ window.setTimeout(function(){ save_them_prontos(); },100) } save_them_prontos(); triple :(
- Cthulhu_ 14y ago$('img').live('mouseover', function() {}); Same, but nicer, listens to mouseover events on img elements that may not exist yet. Then again, this will - if I understand correctly - cause the event to bubble up in the DOM until it reaches the root element, where jquery's live callback handler will check what element the event took place on and invoke that callback.
- lazerwalker 14y agoYou're spot on that's the cleanest conceptual way to do it in jQuery. In more recent versions of jQuery, though, $.live is technically deprecated (1.7+). The equivalent new syntax is: $(document).on('mouseover', 'img', function() {});
- jpwagner 14y agothe 5 previous posts in this thread are so perfectly stepwise, it's making my head explode.
- zaius 14y agoThat creates a event handler for all images on mouseover, even images that haven't been created yet. You want to trigger the mouseover event, not bind to it. And you want to trigger it after the image has been inserted. Unfortunately there's no jquery event for dom insertions. But on firefox you can use a MutationObserver: MutationObserver(function(event) { $(event[0].addedNodes[0]).mouseover(); }).observe($('body')[0], {childList: true});
- SonicSoul 14y agosetInterval( function() { var ims = document.getElementsByTagName('img'); var event = document.createEvent("MouseEvent"); event.initMouseEvent("mouseover", true, true, window); for(var i=0;i<ims.length; i++){ ims[i].dispatchEvent(event); }}, 2000 )