<%@ 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>