How to modify an image's attr use jquery? I want set the image's height as the window.height
<script type="text/livescript">
$(document).ready(function(){
var height = $(window).height();
$('img').attr('height',height);
});
</script>
<img src="http://farm7.static.flickr.com/6124/6022097678_4477a09976_o.jpg" />
-
Your fiddle works perfectly for me!James Allardice– James Allardice2011年09月16日 09:24:48 +00:00Commented Sep 16, 2011 at 9:24
2 Answers 2
I think you could use:
$('img').attr('height', $(window).height());
Or you could use prop() in place of attr():
$('img').prop('height', $(window).height());
But both of these are variations on what you've already written, the problem you're having is that you've used type="text/livescript" in your style tag, if you amend that to: style="text/javascript" it works.
References:
Comments
You should not change the height attribute, you should change the height css.
And for window height in particular you can just do:
html, body {
height: 100%;
}
Then add:
style="height: 100%;"
To your image and you don't need to mess with jQuery at all.