javascript - Query text() over dynamically added img -
i realize it's answered question, wanted ask, if there new on market, can solve issue.
i'm loading images specified coordinates using javascript.
var span = document.createelement("span"); span.innerhtml = "<img src=\"source.gif\" id=\"someid\">"; span.style.position = "absolute"; span.style.left = coodinatesleft[i] + "px"; span.style.top = coodinatestop[i] + "px"; document.body.appendchild(span);
later on, when mouse goes on images want use simple mouseenter function in jquery
$("mydivid").mouseenter(function(){ $(this).text("something"); });
to write text there, image is. , don't text, suppose, because image more foreground text. other times i've used it, , worked, had div, , no actual image source on it.
is there way keep structure , append text on image?
ps: read z-index, not sure if need, since don't have pre-defined css class images.
thanks in advance!
you should use event delegation
dynamically added elements.
$('<span />') .html('<img src="source.gif" class="myimages">') .css({ 'position': 'absolute', 'left': coodinatesleft[i] + 'px', 'top': coodinatestop[i] + 'px' }) .appendto('body'); $('.myimages').hover(function() { $(this).append('<span id="mytext">my text here</span>'); }, function() { $('#mytext').remove(); });
css:
#mytext { height: same .myimages; width: same .myimages; opacity: .5; background: #ccc; }
Comments
Post a Comment