Jquery -添加动态变量的顶部

Jquery - Adding on top of dynamic variable

本文关键字:顶部 变量 动态 添加 Jquery      更新时间:2023-09-26

所以我有下面的代码,这对我来说很好!

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

但是我的问题是,我想能够添加15个像素的空白,但下面的代码不工作。

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

我假设这是因为输出将是'260px15'而不是'275px',这是我的目标。我想知道是否有一种方法可以简单地做到这一点,我看得太多了?

下面将为您工作!

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

parseInt()可以从字符串末尾删除'px'并将其转换为整数。

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