简体   繁体   English

jQuery-在动态变量之上添加

[英]Jquery - Adding on top of dynamic variable

So I have the below code that is working great for me! 因此,我有以下代码对我来说非常有用!

$("#custom_logo").css("marginLeft", $(".rt-container:first").css("marginLeft"));

But my problem is that I want to be able to add 15 more pixels to the margin but the below code does not work at all. 但是我的问题是我希望能够在边缘增加15个像素,但是下面的代码根本不起作用。

$("#custom_logo").css("marginLeft", $(".rt-container:first").css("marginLeft")+15); 

I am presuming that is it because the output will be somthing like '260px15' rather than '275px' which is what I am aiming for. 我想是这样,因为输出将是“ 260px15”之类的东西,而不是我想要的“ 275px”。 I was wondering if there was a way to do this simply that I am over looking? 我想知道是否有一种简单的方法可以解决我的问题?

The following will work for you! 以下内容将为您服务!

$("#custom_logo").css("marginLeft", parseInt($(".rt-container:first").css("marginLeft")) +15);

parseInt() can remove the 'px' from the end of the string and convert to an integer. parseInt()可以从字符串末尾删除“ px”并将其转换为整数。

$("#custom_logo").css("marginLeft",

 ($(".rt-container:first").css("marginLeft").slice(0,-2) +15 )* 1)

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

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