How to modify an image's attr use jquery? I want set the image's height
as the window.height
http://jsfiddle.net/wyCqT/
<script type="text/livescript">
$(document).ready(function(){
var height = $(window).height();
$('img').attr('height',height);
});
</script>
<img src="http://farm7.static.flick开发者_如何学JAVAr.com/6124/6022097678_4477a09976_o.jpg" />
I think you could use:
$('img').attr('height', $(window).height());
JS FIddle demo
Or you could use prop()
in place of attr()
:
$('img').prop('height', $(window).height());
JS Fiddle demo
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:
attr()
.prop()
.height()
.
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.
精彩评论