开发者

jquery css strings substrings

开发者 https://www.devze.com 2023-03-08 23:35 出处:网络
var currentLeft = $开发者_如何学编程(this).css(\'left\'); if((currentLeft%2)== 0){ thisLeft = currentLeft; }
var currentLeft = $开发者_如何学编程(this).css('left');

if((currentLeft%2)== 0){ thisLeft = currentLeft; }

I want to set the variable thisLeft = currentLeft only if the left attribute of the clicked item is divisible by 0.

The problem is that css('left') selects '400px', but im only interested in the 400 part.

How can i do this?


var currentLeft = $(this).css('left');
    currentLeft = currentLeft.replace('px','');
if((currentLeft%2)== 0){ thisLeft = currentLeft; }


var currentLeft = parseInt($(this).css('left').replace("px", ""));


Get rid of the px.

var currentLeft = $(this).css('left').replace('px','');


you can also try with jQuery.position() for example:

CSS

div { padding: 15px;}
p { margin-left:10px; }

HTML

<div>
  <p>Hello</p>
</div>

jQuery

var position = p.position();
$("p:last").text( "left: " + position.left + ", top: " + position.top );

will return left: 15, top: 15

0

精彩评论

暂无评论...
验证码 换一张
取 消