So I have some code that uses JavaScript templates to create some HTML, including some <img />
tags. For our purposes, it looks like this:
$("myButton").click(function() { $("myDiv").html(
'lots of html included <img class="A" /> tags');
Now, I'd like to have a method that replaces all <img />
that have class="A"
with a <div />
with appropriate background-images and the like. (This is a hack to use border-radius
to make square images into round ones.) This method looks roughly like this:
function replaceA()
{
var imageUri = $(this).attr("src");
$(this).replaceWith('<div class="B" style="background-image: url(' + imageUri + ')"></div>');
}
But here's the catch. I want to run this method on all img.A
s, no matter how they show up on my site---whether in the HTML, or inserted through a template method like the above, or even loaded with Ajax $("myDiv").load(...)
. And this appears to be nontrivial, since even with $(".A").live(...)
, the event handler never gets fired. In short, the following code does not work:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<div id="MyDiv"></div>
<script>
$(".A").live("load", function ()
{
alert(this);
});
$("#MyDiv").html('stuff <img class="A" src="http://code.google.com/images/code_logo.png" /> more stuff');
</script>
Any ideas as to how I can perform this trick?