JavaScript Tutorial/jQuery/hover
Содержание
Hover action
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("b").hover(function () {
$(this).css({"background-color" : "yellow", "font-weight" : "bolder"});
}, function () {
var cssObj = {
"background-color" : "#ddd",
"font-weight" : "",
"color" : "red"
}
$(this).css(cssObj);
});
});
</script>
</head>
<body>
<body>
<b>Hello</b>
</body>
</html>
hover(over, out): Whenever mouse cursor is moved over a matched element, the first specified function is fired.
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("li").hover(
function () {
$(this).append($("<span>*</span>"));
},
function () {
$(this).find("span:last").remove();
}
);
});
</script>
</head>
<body>
<body>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
</body>
</html>
Hover to add and remove class
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").hover(
function () {
$(this).addClass("my");
},
function () {
$(this).removeClass("my");
}
);
});
</script>
<style>
.my { color:blue; }
</style>
</head>
<body>
<body>
<div>header 1</div>
</body>
</html>
Hover to fade in and out
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});
});
</script>
</head>
<body>
<body>
<div><h1>header 1</h1></div>
</body>
</html>
Hover to hide tag
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").hover(
function () {
$(this).append($("<span> ***</span>"));
},
function () {
$(this).find("span:last").remove();
}
);
});
</script>
</head>
<body>
<body>
<div><h1>header 1</h1></div>
</body>
</html>
Tag hover function
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var newText = $("p").text().split(" ").join("</div> <div>");
$("p").html(newText).find("div").hover(
function () { $(this).addClass("y"); },
function () { $(this).removeClass("y"); });
});
</script>
<style>
.y { background:yellow; }
</style>
</head>
<body>
<body>
<p>a ab abc</p>
</body>
</html>