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
精彩评论