Javascript 如何去除高度和宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4813085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove height and width?
提问by rajeshrt
currently I am working on an image slider with jquery. I have downloaded the code from the net. My code demo is here.
目前我正在使用 jquery 处理图像滑块。我已经从网上下载了代码。我的代码演示在这里。
My question is: how can I remove the height and width attaching dynamically to image tag as inline style?
我的问题是:如何删除作为内联样式动态附加到图像标签的高度和宽度?
Thanks.
谢谢。
回答by Olegas
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'
});
回答by Aristos
To make it work from all ways, you need to remove both attribute and css. This is work on my code.
为了使其从各个方面工作,您需要同时删除属性和 css。这是我的代码的工作。
$('your-image')
.removeAttr("width").removeAttr("height")
.css({ width: "", height: "" });
回答by Mitch
This is a VERY simple solution with jQuery. I found the answer at wpwizard.net.
这是一个非常简单的 jQuery 解决方案。我在 wpwizard.net 上找到了答案。
Here's the URL: http://wpwizard.net/jquery/remove-img-height-and-width-with-jquery/
这是网址:http: //wpwizard.net/jquery/remove-img-height-and-width-with-jquery/
Here's the jQuery:
这是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>
回答by Nelu
$('img').width('auto').height('auto');
回答by fancab
inherit could be one of the way.
继承可能是其中一种方式。
$('your-image').css({ width: "inherit", height: "inherit" });
回答by Grant
You can do it with vanilla JavaScript too.
你也可以用普通的 JavaScript 来做到这一点。
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";
}