JavaScript DHTML/jQuery/toggle
Содержание
- 1 Click to toggle opacity
- 2 Click to toggle style
- 3 Normal toggle
- 4 Slow toggle
- 5 Toggle among two or more function calls every other click
- 6 Toggle fast
- 7 toggle(fn, fn1): when clicking matched element, the first specified function is fired, clicked again, the second is fired.
- 8 Toggle in milliseconds
- 9 Toggle true and false
- 10 Use toggle function to change class
Click to toggle opacity
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#my").toggle(
function(event) {
$(event.target).css("opacity",0.4);
},
function(event) {
$(event.target).css("opacity",1.0);
}
);
});
</script>
</head>
<body>
<body>
<div id="my">asdf</div>
</body>
</html>
Click to toggle style
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function () {
$("p").toggle();
});
});
</script>
</head>
<body>
<body>
<p>Hello</p>
</body>
</html>
Normal toggle
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function () {
$("p").toggle("normal");
});
});
</script>
</head>
<body>
<body>
<p>Hello</p>
</body>
</html>
Slow toggle
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function () {
$("p").toggle("slow");
});
});
</script>
</head>
<body>
<body>
<p>Hello</p>
</body>
</html>
Toggle among two or more function calls every other click
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").toggle(
function () {
$(this).css({"color":"blue"});
},
function () {
$(this).css({"color":"red"});
},
function () {
$(this).css({"color":"yellow"});
}
);
});
</script>
</head>
<body>
<body>
<div>header 1</div>
</body>
</html>
Toggle fast
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function () {
$("p").toggle("fast");
});
});
</script>
</head>
<body>
<body>
<p>Hello</p>
</body>
</html>
toggle(fn, fn1): when clicking matched element, the first specified function is fired, clicked again, the second is fired.
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("li").toggle(
function () {
$(this).css("list-style-type", "disc")
.css("color", "blue");
},
function () {
$(this).css({"list-style-type":"", "color":""});
}
);
});
</script>
</head>
<body>
<body>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
</body>
</html>
Toggle in milliseconds
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function () {
$("p").toggle(1000);
});
});
</script>
</head>
<body>
<body>
<p>Hello</p>
</body>
</html>
Toggle true and false
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var c = 0;
$("p").click(function () {
$("p").toggle( c++ % 2 == 0 );
});
});
</script>
</head>
<body>
<body>
<p>Hello</p>
</body>
</html>
Use toggle function to change class
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").toggle(
function () {
$(this).addClass("my");
},
function () {
$(this).removeClass("my");
}
);
});
</script>
<style>
.my { color:blue; }
</style>
</head>
<body>
<body>
<div>header 1</div>
</body>
</html>