上面对hover事件进行了练习,他是对鼠标的移进和移出做不同的响应。而toggle则是对鼠标的每一次点击做不同的响应,直到方法执行完为止。
toggle定义的执行方法
$("监听的ID").toggle(
function(){
方法体1;
}
function(){
方法体2;
}
function(){
方法体3;
}
);
eg:
<head>
</head>
<script type="text/javascript" src="jquery.js"></script>
<style type="text/css">
.t1{
color:red;
background-color: blue;
}
.t2{
color:blue;
background-color:red;
}
</style>
<script type="text/javascript">
$(function(){
$("#test").toggle(
function(){
$("#test").addClass("t1");
},
function(){
$("#test").addClass("t2");
}
);
});
</script>
<body>
<input type="button" id="test" value="点击">
</body>
</html>
本文通过一个具体的实例详细介绍了如何使用 jQuery 中的 toggle 方法来实现元素样式的切换效果。每次点击按钮时,元素将在预定义的两种样式之间进行切换。

5330

被折叠的 条评论
为什么被折叠?



