我的 HTML 中有一张图片,我正在尝试根据它是纵向还是横向的事实来更改它的尺寸,以免造成太多失真。我有 3 个按钮“小”、“中”、“大”,每个按钮都会改变高度和宽度。
这是我写的 if 语句,但是点击按钮没有任何反应
$('#large').click(function() {
var height = document.getElementById('#uploadedPhoto').clientHeight;
var width = document.getElementById('#uploadedPhoto').clientWidth;
if (height > width) {
$('#upoladedPhoto').css('height', '95%');
$('#upoladedPhoto').css('width', '85%');
}
else if (height < width) {
$('#upoladedPhoto').css('height', '85%');
$('#upoladedPhoto').css('width', '95%');
}
else {
$('#upoladedPhoto').css('height', '95%');
$('#upoladedPhoto').css('width', '95%');
}
});
这是上传图片的css/
#upoladedPhoto {
display:none;
position:relative;
cursor:move;
max-height:100%;
max-width:100%;
left: 5em;
opacity:0.75;
}
3 个按钮在没有 if 语句的情况下工作,但在那种情况下我只更改 css 属性而不考虑它是处于纵向模式还是横向模式
如果有更好的尝试方法,请分享。
谢谢
最佳答案
你正在将 javascript
与 jquery
混合,
会是
document.getElementById('uploadedPhoto').clientHeight;
代替
document.getElementById('#uploadedPhoto').clientHeight;
而且我认为关于 clientHeight
阅读 here 存在一些问题& here .
你可以使用简单的jquery
$('#uploadedPhoto').height();
关于javascript - <div> 高度在 if 语句中不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25228462/