无法使用整型字符串作为像素值



在下面的代码中,我试图让字符串(40*(parseInt($('#2-cat').attr('id')))+1)成为移动div的像素高度。但它只是不工作,我真的不明白为什么-我已经放入了另外两个测试行代码,服务器似乎接受这个字符串是一个整数…但是它不允许我用它来设置div的高度

$(document).ready(function() {
    $('p').click(function(){
        //$('p').html(40*(parseInt($('#2-cat').attr('id')))+5)
        //$("#line").animate({"top":"80px"});
        $("#line").animate({"top":"(40*(parseInt($('#2-cat').attr('id')))+1)px"});
    });
});
HTML:

<div id='line' style='height:100px; width:100px; background-color:red;position:relative'></div>
<div id='2-cat'>hello</div>
<p>hellos</p>

可在http://jsfiddle.net/platypus117/XyLNG/1/查看

你正在给animate方法传递一个大字符串作为你的最大值。

它不知道如何处理它。

你需要先计算顶部的值,然后把它传递给animate方法。

http://jsfiddle.net/XyLNG/3/

最新更新