源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JQuery控制DIV的选取实现方法

  • 时间:2020-12-21 22:55 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JQuery控制DIV的选取实现方法
我们设置4个div 当鼠标移动到某一个div上面的时候 背景颜色就会发生变化 那我们应该知道要用到mouseover() 和 mouseout() 前一个是移动到某个位置 后面是移开某个位置 [b]还有一个hover(,) 可以替代上面两个方法[/b] [b]1.用mouseover()和mouseout()[/b]
<!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>
[img]http://files.jb51.net/file_images/article/201609/20160317081242833.jpg[/img] [img]http://files.jb51.net/file_images/article/201609/20160317081344602.jpg[/img] [img]http://files.jb51.net/file_images/article/201609/20160317081353365.jpg[/img] [b]2.hover() [/b]
<!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>
[img]http://files.jb51.net/file_images/article/201609/20160317081850601.jpg[/img] 也可以这么写
$(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的选取实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部