JQuery动画div的宽度onclick

时间:2014-02-01 07:37:59

标签: jquery css html jquery-animate width

我不知道为什么这不起作用。

JQuery的:

$("#foldit").click(function () {
    $("foldit").animate({"width": "165px"}, "fast");
});

3 个答案:

答案 0 :(得分:5)

这是因为您在选择器中错过了#

尝试使用该点击事件中的this引用来实现您想要的效果,

$("#foldit").click(function () {
    $(this).animate({"width": "165px"}, "fast");
});

根据您的新要求,您可以尝试这样做,

$('#foldit').click( function() {
    var toggleWidth = $(this).width() == 165 ? "100px" : "165px";
    $(this).animate({ width: toggleWidth });
});

DEMO

答案 1 :(得分:2)

您在选择器中错过了'#',您也可以像这样使用它

$("#foldit").click(function () {
    $(this).animate({"width": "165px"}, "fast");
});

答案 2 :(得分:2)

$(this)

的位置使用$("foldit")