Display an image in a <div>, jQuery
The value of ig is being transfered fine, but I need to put this image into an image, please tell me what I am doing wrong.
var ig = '<img src='+$(this).attr('src')+' 开发者_运维百科style="z-index:1">';
$('#big').val(this)
Thanks Jean
you need to use.html instead,
var ig = '<img src='+$(this).attr('src')+' style="z-index:1">';
$('#big').html(this)
var ig = $('<img />').attr('src', $(this).attr('src')).css('z-index', 1);
$('#big').append(ig);
Use .html
instead of .val
and pass ig
. .val
is used for manipulating form elements.
You want to use html
or appendTo
instead of val
.
var ig = '<img src='+$(this).attr('src')+' style="z-index:1">';
$('#big').html(ig); // replaces contents
or
$(ig).appendTo('#big'); // appends to existing contents
Use append instead of val
var ig = '<img src='+$(this).attr('src')+' style="z-index:1">';
$('#big').append(ig);
working example: http://jsbin.com/agoye3
Incidentally, if #big is already an image, you can just change the image source:
<img id='big' src='theoldimage.jpg'>
<script>
$('#big').attr("src", 'thenewimage.jpg');
</script>
精彩评论