是否可以从JavaScript注入的内联样式中减去一个数字? [英] Is it possible to subtract a number from inline style injected by JavaScript?
本文介绍了是否可以从JavaScript注入的内联样式中减去一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在HTML中添加了内联样式,例如style="left: 10px;"
.我可以在该数字上加减吗?
I have an inline style added to the HTML, like style="left: 10px;"
. Can I add/subtract to that number?
我想创建一个规则,无论该数字是多少,都可以从该数字中删除8px
.
I would like to create a rule where I can remove 8px
from that number no matter what that number is.
我尝试使用难看的!important
hack覆盖它,但是当初始值更改时,这无济于事.
I tried using the ugly !important
hack to override it, but that doesn't help when the initial value is changed.
推荐答案
从1.6开始,您可以使用便捷的方法:
From 1.6 onwards, you can use the convenient:
$('#myelem').css('left','-=8px')
如果由于某种奇怪的原因而被困在过去,请使用:
If for some strange reason you are stuck in the past, use:
$('#myelem').css('left', function(i,v) {
return v - 8;
});
对于jQuery 1.4及更高版本.
for jQuery 1.4 up.
这篇关于是否可以从JavaScript注入的内联样式中减去一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文