<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'attrandaddClass.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.8.0.js"></script>
<style type="text/css">
.high{
font-weight:bold;
color:red;
}
.another{
font-style:italic;
color:green;
}
</style>
<script type="text/javascript">
$(document).ready(function()
{
$("#button1").click(function()
{
$("p").attr('class',"high");//属性的方式添加属性,若已有属性会覆盖
});
$("#button2").click(function()
{
console.log($("p").attr('class'));//显示属性
});
$("#button3").click(function()
{
$("p").addClass("another");//只是增加了个class的样式,若原来无,增加属性和样式,若有,追加样式
});
$("#button4").click(function()
{
$("p").removeClass("another");//移除一个class的样式,
});
$("#button5").click(function()
{
$("p").removeClass();//移除class,全部样式移除
});
$("#button6").click(function()
{
$("p").toggleClass("another");//在切换中变换样式
});
$("#button7").click(function()
{
console.log($("p").hasClass("another"));//是否有这样的样式
});
$("#button8").click(function()
{
console.log($("p").is(".another"));//是否有这样的样式或属性
});
});
</script>
</head>
<body>
<p>感觉自己写代码的速度好慢啊?</p>
<ul>
<li>疯狂的写代码</li>
<li>小郁闷啊</li>
<li>感觉好无缘啊</li>
<li>哎,该怎么办呢?</li>
</ul>
<input type="button" id="button1" value="attr(2)">
<input type="button" id="button2" value="attr()">
<input type="button" id="button3" value="addClass()">
<input type="button" id="button4" value="removeClass(2)">
<input type="button" id="button5" value="removeClass()">
<input type="button" id="button6" value="toggleClass()">
<input type="button" id="button7" value="hasClass()">
<input type="button" id="button8" value="is()">
</body>
</html>