How to remove height and width?
currently I am working on an image slider with jquery. I have downloaded the code fro开发者_StackOverflow中文版m the net. My code demo is here.
My question is: how can I remove the height and width attaching dynamically to image tag as inline style?
Thanks.
Try to use this code:
$('your-image').css({
width: '',
height: ''
});
If you want to set the "original" image dimensions try this:
$('your-image').css({
width: 'auto',
height: 'auto'
});
To make it work from all ways, you need to remove both attribute and css. This is work on my code.
$('your-image')
.removeAttr("width").removeAttr("height")
.css({ width: "", height: "" });
This is a VERY simple solution with jQuery. I found the answer at wpwizard.net.
Here's the URL: http://wpwizard.net/jquery/remove-img-height-and-width-with-jquery/
Here's the jQuery:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function($){
$('img').each(function(){
$(this).removeAttr('width')
$(this).removeAttr('height');
});
});
</script>
You can do it with vanilla JavaScript too.
let articleContentImages = document.getElementById("articleContent").getElementsByTagName("img");
for(let i = 0; i < articleContentImages.length; i++){
articleContentImages[i].style.height = "auto";
articleContentImages[i].style.width = "auto";
}
$('img').width('auto').height('auto');
inherit could be one of the way.
$('your-image').css({ width: "inherit", height: "inherit" });
精彩评论