正确答案。
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>挑战题</title> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script> <style> div { background-color: gray; width: 20px; height: 20px; } </style> </head> <body> <div></div> <input type="button" id="slideL" value="l"/> <input type="button" id="slideR" value="r"/> </body> <script type="text/javascript"> $(function () { var di=parseInt($("div").css("margin-left")); $("#slideL").bind("click", function () { console.log($(this).css("margin-left")); $("div").animate({ "margin-left": di-=50 }, 400); }); $("#slideR").bind("click", function () { $("div").animate({ "margin-left": di+=50 }, 400); }); } ) </script>
大家互相学习