单击实际元素,开始动画
问题描述:
这里是我的HTML单击实际元素,开始动画
<div id="test">There I dont want put button</div>
和我的CSS
#test { width: 300px; height: 100px; }
和脚本
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("#test").animate({height:300},"slow");
});
});
</script>
我怎样才能激活此动画无使用按钮?我想点击div #test
并将其设置为300x300。我尝试使用链接和标签,但似乎点击处理程序只理解按钮单击。
答
可以直接给click事件处理程序的div †:
$(function() {
$('#test').click(function() {
$(this).animate({height:300},"slow");
});
});
然后当您单击DIV,动画将开始。
或者,如果你想直接运行动画加载页面时,直接调用动画功能:
$(function() {
$("#test").animate({height:300},"slow");
});
†:与用户的交互,例如click
活动, mouseover
等与一起使用全部元素。但还有其他事件,例如load
或change
,它们只能由某些元素生成。
答
没有,click事件可以采取的任何元素:
$(document).ready(function(){
$("#test").click(function(){
$("#test").animate({height:300},"slow");
});
});
看到一个工作演示: http://jsfiddle.net/e4ceD/
答
$('#test').click(function(){
$(this).animate({height:300},"slow");
});
看看[这个问题](http://stackoverflow.com/questions/7636103/jquery-animation-on-window-load-on-an-infinite-loop)就是你在找的东西。 – Alex 2012-03-04 14:44:59