English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JQuery를 사용하여 DIV 선택을 구현하는 방법

우리는 설정합니다4div이 있을 때鼠标移动到某一个div上面的时候 배경색이 변합니다.

그러면mouseover()와mouseout()를 알아야 합니다. 전자는 특정 위치로 이동하는 것이고, 후자는 특정 위치에서 벗어나는 것입니다.

그리고hover(,)를 사용할 수 있습니다.

1.용mouseover()와mouseout()

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jQuery/jquery-1.12.1.js"></script>
<script type="text/javascript">
$(function(){
	$("div[id^='div']").mouseover(function() {
    $(this).css("background-color","red");
    //$("this").css({"background-color":"red"});
 });
	$("div[id^='div']").mouseout(function() {
    $(this).css("background-color","#0FC");
    //$("this").css({"background-color":"#0FC"});
 });
});
</script>
<style type="text/css">
div[id^="div"]
{
	width:300px;
	height:500px;
	background-color:#0FC;
	border:1px solid black;
	float:left;
	margin-left:2px;
}
</style>
</head>
<body>
<div id="div1></div>
<div id="div2></div>
<div id="div3></div>
<div id="div4></div>
</body>
</html>

2.hover()

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jQuery/jquery-1.12.1.js"></script>
<script type="text/javascript">
$(function(){
	$("div[id^='div']").hover(function(){
		$(this).css("background-color","blue");
		},
		function(){
			$(this).css("background-color","#0FC");
	});
});
</script>
<style type="text/css">
div[id^="div"]
{
	width:300px;
	height:500px;
	background-color:#0FC;
	border:1px solid black;
	float:left;
	margin-left:2px;
}
</style>
</head>
<body>
<div id="div1></div>
<div id="div2></div>
<div id="div3></div>
<div id="div4></div>
</body>
</html>

이렇게도 쓸 수 있습니다

$(function(){
	var In = function(){
		$(this).css("background-color","red");
	}
	var Out = function(){
		$(this).css("background-color","yellow");
	}
	$("p[id^='p']").hover(In,Out);	
});

이 글에서 설명한 JQuery로 DIV 선택을 제어하는 방법이 저의 공유한 모든 내용입니다. 여러분에게 참고가 되길 바라며,呐喊 교본에 많은 지지를 부탁드립니다.

좋아하는 것