zl程序教程

您现在的位置是:首页 >  后端

当前栏目

jQuerytoggleClass应用实例(附效果图)

实例应用 效果图
2023-06-13 09:15:24 时间
1、首先到jQuery官网下载js库,网址为http://jquery.com/

2、建立一个jQuery示例的项目。

3、将js库放到jQuery示例的项目中。

4、写一个html页面
复制代码代码如下:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>
<metahttp-equiv="author"content="Kong.Yee"/>
<metahttp-equiv="corporation"content="广州匡兴软件科技有限公司"/>
<metahttp-equiv="contact"content="791663094或kong.yee@foxmail.com"/>
<scripttype="text/javascript"language="JavaScript"src="js/jquery-1.10.2.js"></script>
<title>Inserttitlehere</title>
<styletype="text/css">
.bg{
/*背景颜色*/
background:#f00;
/*字体颜色*/
color:#fff;
width:80px;
}

ul,li{
/*清除ul和li上默认的小圆点*/
list-style:none;
}

ul{
/*清除子菜单的缩进值*/
padding:0;/*IE8,IE9,火狐可以;IE7,IE6,IE5.5不行*/
margin:0;/*都可以了*/
}

</style>
<scripttype="text/javascript">
$(function(){
//setColor是鼠标移动的方法
$("li").mouseover(setColor).mouseout(setColor);

functionsetColor(){
//如果存在(不存在)就删除(添加)一个类
$(this).toggleClass("bg");
}
});
</script>
</head>
<body>
<divid="div">
<ul>
<li>横向菜单1</li>
<li>横向菜单2</li>
<li>横向菜单3</li>
<li>横向菜单4</li>
<li>横向菜单5</li>
<li>横向菜单6</li>
</ul>
</div>
</body>
</html>

5、运行效果图