简体   繁体   English

Javascript从百分比中减去像素

[英]Javascript subtracting pixels from percentage

I am looking to use JavaScript to restyle a div on button click. 我希望使用JavaScript在按钮单击时重新设置div的样式。 I have everything working, except one thing. 除了一件事情,我一切正常。 One of the things that I want to do is change the maximum height of the div from what I currently have it set to, to 100% - 12px. 我要做的一件事是将div的最大高度从当前设置的高度更改为100%-12px。 I tried using the CSS calc() function, but it didn't work. 我尝试使用CSS calc()函数,但是没有用。 The strangest thing about this is that when I checked the developer console for errors, it did not throw an error. 最奇怪的是,当我检查开发人员控制台中的错误时,它没有抛出错误。 Does anyone know how to do what I am asking? 有人知道我该怎么做吗?

Thanks in advance! 提前致谢!

I would solve this problem using jQuery by adding a click binding to your button and then getting the height of .parent(). 我将使用jQuery解决此问题,方法是在您的按钮上添加单击绑定,然后获取.parent()的高度。 I've demonstrated this in this fiddle http://jsfiddle.net/TGyYv/ . 我已经在这个小提琴http://jsfiddle.net/TGyYv/中对此进行了演示。

The key is this code. 关键是此代码。

$("#button").click(function () {
    parent_height = $(this).parent().height()
    $(this).height(parent_height - 12)
})

When the button is clicked, it gets the parent's height, then sets the button's height to the parent's minus 12! 单击按钮时,它将获得父对象的高度,然后将按钮的高度设置为父对象的负12!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM