jQuery addClass removeClass toggleClass hasClass is(.class)用法

jQuery addClass removeClass toggleClass hasClass is(.class)用法

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta charset="UTF-8">
<title>b index</title>
<link rel='stylesheet' type="text/css" href='b/css/bootstrap.css'>
<style type="text/css">
    .s1{
        width: 100px;
        height: 100px;
        background-color: pink;
    }
    .s2{
        width: 50px;
        height: 50px;
        background-color: green;
    }
</style>
</head>
<body style="margin:150px;">
    <div id="div001">div001</div>
    <div id="div002">div002</div>
    <div id="div003">div003</div>
    <div>
        <button id="btn001">click me to get btn001 class </button>
        <button id="btn002">click me to add class s1 </button>
        <button id="btn003">click me to add class s1 s2 </button>
        <button id="btn004">click me to remove class </button>
        <button id="btn005">click me to remove class s1 </button>
        <button id="btn006">click me to toggleClass s1 </button>
        <button id="btn007">click me to hasClass s1 </button>
    </div>
    <script type="text/javascript" src="js/jquery-1.11.1.js"></script>
    <script type="text/javascript" src="b/js/bootstrap.js"></script>
    <script type="text/javascript" src="js/index034.js"></script>
</body>
</html>
$(function() {
    $('#btn001').click(btn001Click);
    $('#btn002').click(btn002Click);
    $('#btn003').click(btn003Click);
    $('#btn004').click(btn004Click);
    $('#btn005').click(btn005Click);
    $('#btn006').click(btn006Click);
    $('#btn007').click(btn007Click);
});
function btn001Click() {
    var string = $('#div001').attr('class');
    console.log(string);
}
function btn002Click() {
    $('#div001').addClass('s1');
}
function btn003Click() {
    // 多个空格会被变为一个;
    $('#div001').addClass('s1   s2');
}
function btn004Click() {
    // 如果有了class,removeClass之后,还是有class属性的,attr('class')得到的会是空字符串;
    $('#div001').removeClass();
}
function btn005Click() {
    // bt2-->bt3 -->bt4-->bt2
    // s1 -->s1 s2 -->s2 --> s2 s1
    // pink->green -->green->green
    // 反复折腾之后s2 s1显示的是green的,不是s1的pink;
    $('#div001').removeClass('s1');
}
function btn006Click() {
    // 反复折腾之后s2 s1显示的是green的,不是s1的pingk;
    $('#div001').toggleClass('s1');
}
function btn007Click() {
    // 反复折腾之后,虽然s1不生效,但是还是能够判断出来有这个类;
    var string=$('#div001').hasClass('s1');
    console.log(string);
    var str2 = $('#div001').is('.s1');
    console.log(str2);
}

 

转载于:https://www.cnblogs.com/stono/p/4949389.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值