day30-jQuery

一、广告窗

代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>广告窗</title>
        <style type="text/css">
            #adv{
                width: 300px;
                height: 200px;
                background-color: powderblue;
                color:#8B4513;
                /*固定定位*/
                position: fixed;
                top: 10px;
                right: 10px;
            }
            
            #adv button{
                width: 50px;
                height: 30px;
                position: absolute;
                right: 0px;
                border: none;
                outline: none;
                background-color: whitesmoke;
            }
        </style>
    </head>
    <body>
        <div id="adv">
            此广告位招租
            <button>关闭</button>
        </div>
        
        <script type="text/javascript">
            var advDiv = document.getElementById('adv');
            var button = document.querySelector('#adv button');
            var counter = 0;
            button.addEventListener('click',function(){
                counter += 1;
                if(counter < 3){
                    //document.defaultView.getComputedStyle(元素) -> 读取元素的所有样式
                    var currentStyle = document.defaultView.getComputedStyle(advDiv);
                    var newTop = parseInt(currentStyle.top) + 20;
                    var newRight = parseInt(currentStyle.right) + 20;
                    advDiv.style.top = newTop + 'px';
                    advDiv.style.right = newRight + 'px';
                }else{
                    advDiv.style.display = 'none';
                }
                
            });
            
            //鼠标按下 - mousedown
            //鼠标移动 - mousemove
            //鼠标松开 - mouseup
            //事件对象  clientX / clienY - 鼠标的横纵坐标
        </script>
    </body>
</html>

测试结果

1.PNG

二、表格操作

代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>jQuery运用</title>
        <style>
            #data {
                
                border-collapse: collapse;
                margin: 0 auto;
                position: relative;
                margin-left: 410px;
                margin-top: 50px;
            }
            
            #data td, #data th {
                width: 120px;
                height: 40px;
                text-align: center;
                border: 1px solid black;
            }
            
            #buttons {
                
                position: relative;
                margin-left: 480px;
                margin-top: 30px;
            }
            
            #buttons button{
                width: 100px;
                height: 40px;
                margin-right: 20px;
                border: none;
                outline: none;
                background-color: antiquewhite;
                font: 18px/18px arial;
            }
        </style>
    </head>
    <body>
        <table id="data">
            <caption>数据统计表</caption>
            <tr>
                <th>姓名</th>
                <th>年龄</th>
                <th>性别</th>
                <th>身高</th>
                <th>体重</th>
            </tr>
            <tr>
                <td>Item1</td>
                <td>Item2</td>
                <td>Item3</td>
                <td>Item4</td>
                <td>Item5</td>
            </tr>
            <tr>
                <td>Item1</td>
                <td>Item2</td>
                <td>Item3</td>
                <td>Item4</td>
                <td>Item5</td>
            </tr>
            <tr>
                <td>Item1</td>
                <td>Item2</td>
                <td>Item3</td>
                <td>Item4</td>
                <td>Item5</td>
            </tr>
            <tr>
                <td>Item1</td>
                <td>Item2</td>
                <td>Item3</td>
                <td>Item4</td>
                <td>Item5</td>
            </tr>
            <tr>
                <td>Item1</td>
                <td>Item2</td>
                <td><a>Item3</a></td>
                <td>Item4</td>
                <td>Item5</td>
            </tr>
            <tr>
                <td>Item1</td>
                <td>Item2</td>
                <td>Item3</td>
                <td>Item4</td>
                <td><a>Item5</a></td>
            </tr>
        </table>
        <div id="buttons">
            <button id="pretty">美化表格</button>
            <button id="clear">清除数据</button>
            <button id="remove">删单元格</button>
            <button id="hide">隐藏表格</button>
        </div>
        
        <!-- jQuery:Wtite Less Do More -->
        <!-- 加载本地的jQuery文件适合开发和测试时使用 -->
        <script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
        <!-- 下面的方式适合商业项目通过CDN服务器来加速获取jQuery的JS文件 -->
        <!-- <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> -->
        
        <script type="text/javascript">
            $(function() {
                $('#pretty').on('click',function(){
                    $('#data tr:gt(0):odd').css({
                        'background-color':'#ccc',
                        'font-size': '20px',
                    });
                    $('#data tr:gt(0):even').css('background-color','#abc');
                });
                
                $('#clear').on('click',function(){
                    //text() -> 文本内容  html() -> 带标签的内容
                    $('#data tr:gt(0)>td').text('');
                });
                
                $('#remove').on('click',function(){
                    $('#data tr:gt(0):last-child').remove();
                });
                    
                $('#hide').on('click',function(){
                    //根据样式表选择器获取元素 获取到的不是原生的JS对象
                    //而是经过jQuery封装过后的对象(有更多的方法方便操作)
                    $('#data').css('visibility','hidden');
                });
            });
        </script>
    </body>
</html>

测试结果

2.PNG

三、添加删除标签

代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>jQuery运用</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
            #container {
                margin: 20px 50px;
            }
            #fruits li {
                list-style: none;
                width: 200px;
                height: 50px;
                font-size: 20px;
                line-height: 50px;
                background-color: cadetblue;
                color: white;
                text-align: center;
                margin: 2px 0;
            }
            #fruits>li>a {
                float: right;
                text-decoration: none;
                color: white;
                position: relative;
                right: 5px;
            }
            #fruits~input {
                border: none;
                outline: none;
                font-size: 18px;
            }
            #fruits~input[type=text] {
                border-bottom: 1px solid darkgray;
                width: 200px;
                height: 50px;
                text-align: center;
            }
            
            /*~兄弟选择器*/
            #fruits~input[type=button] {
                width: 80px;
                height: 30px;
                background-color: coral;
                color: white;
                vertical-align: bottom;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <div id="container">
            <ul id="fruits">
                <!-- a标签有默认的跳转页面的行为,有两种方法可以阻止它的默认行为
                    <a href="javascript:void(0);"> -> 去除a标签的默认行为
                -->
                <li>苹果<a href="">×</a></li>
                <li>香蕉<a href="">×</a></li>
                <li>火龙果<a href="">×</a></li>
                <li>西瓜<a href="">×</a></li>
            </ul>
            <input id="name" type="text" name="fruit">
            <input id="ok" type="button" value="确定">
        </div>
        
        <script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript">
            function removeItem(evt){
                //$函数的第四种用法:参数是原生JavaScript对象
                //将原生JS对象包装成对应的jQuery对象
                $(evt.target).parent().remove();
                evt.preventDefault();
            }
            
            //$函数的第一种用法:$函数的参数是另一个函数
            //传入的函数是页面加载完成之后要执行的
            $(function(){
                //$函数的第二种用法:参数是一个选择器字符串
                //获取元素并得到与之对应的jQuery对象(伪数组)
                $('#fruits a').on('click',removeItem);
                $('#ok').on('click',function(){
                    var friutName = $('#name').val().trim();
                    if(friutName.length > 0){
                        $('#fruits').append(
                            //$函数的第三种用法:参数是一个标签字符串
                            //创建新元素并得到与之对应的jQuery对象
                            $('<li>').text(friutName).append(
                                $('<a>').attr('href','').text('×').on('click',removeItem)
                                )
                        );
                    }
                    //对jQuery对象使用下标运算或get(index)方法会得到原生JS对象
                    $('#name').val('').get(0).focus();
                });
            });
        </script>
    </body>
</html>

测试结果

3.PNG

四、作业

代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>作业</title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            
            #div1{
                width: 350px;
                height: 350px;
                background-color: crimson;
                position: absolute;
                left: 0px;
                top: 0px;
                
            }
            
            #div2{
                width: 300px;
                height: 300px;
                background-color: lawngreen;
                position: absolute;
                left: 0px;
                top: 0px;
                
            }
            
            #div3{
                width: 250px;
                height: 250px;
                background-color: cornflowerblue;
                position: absolute;
                left: 0px;
                top: 0px;
            }
        </style>
    </head>
    <body>
                <div id="div1"></div>
                <div id="div2"></div>
                <div id="div3"></div>
    
        <script type="text/javascript">
            function mouseDown(evt1){
                div = evt1.target;
                currentStyle = document.defaultView.getComputedStyle(div);
                divWidth = parseInt(currentStyle.width);
                divHeight = parseInt(currentStyle.height);
                divX = parseInt(currentStyle.left);
                divY = parseInt(currentStyle.top);
                mouseX = evt1.clientX;
                mouseY = evt1.clientY;
                div.addEventListener('mousemove',mouseMove);
                div.addEventListener('mouseup',mouseUp);    
            }
        
            function mouseMove(evt2){
                currentMouseX = evt2.clientX;
                currentMouseY = evt2.clientY;
                moveX = currentMouseX - mouseX;
                moveY = currentMouseY - mouseY;
                if(divX + moveX < 0 && divY + moveY < 0){
                    div.style.left = '0px';
                    div.style.top = '0px';
                }else if(divX + moveX < 0){
                    div.style.left = '0px';
                    div.style.top = divY + moveY + 'px';
                }else if(divX + moveX > window.innerWidth - divWidth && divY + moveY < 0){
                    div.style.left = window.innerWidth - divWidth + 'px';
                    div.style.top = '0px';
                }else if(divX + moveX > window.innerWidth - divWidth){
                    div.style.left = window.innerWidth - divWidth + 'px';
                    div.style.top = divY + moveY + 'px';
                }else if(divY + moveY < 0){
                    div.style.left = divX + moveX + 'px';
                    div.style.top = '0px';
                }else{
                    div.style.left = divX + moveX + 'px';
                    div.style.top = divY + moveY + 'px';
                }
            }
        
            function mouseUp(evt3){
                div.removeEventListener('mousemove',mouseMove);
            }
            
            var div1 = document.getElementById('div1');
            div1.addEventListener('mousedown',mouseDown);
            var div2 = document.getElementById('div2');
            div2.addEventListener('mousedown',mouseDown);
            var div3 = document.getElementById('div3');
            div3.addEventListener('mousedown',mouseDown);

        </script>
    </body>
</html>

测试结果

4.PNG

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    X先生_未知数的X阅读 15,960评论 3 119
  • 接着再说说刚追完的剧,虽然是十三年前拍的,可对今天仍然有着现实的意义。 为什么两个都想跟对方好好过日子的人,最终的...
    莲花凌子阅读 623评论 0 0
  • 换肤一般分为两种:插件换肤和主题换肤 插件换肤就是所谓的apk换肤,将需要更换的属性写好打成apk包放在asset...
    码农陈小二阅读 507评论 0 3
  • 2017。 这是一个整年。我的推论过程是这样的,2+1+7=10,所以这明显的是一个整年, 多么清晰明朗!如果你没...
    轩轩糖阅读 156评论 0 0
  • 我和你一样,也有许多迷茫的时候。未来遥遥无期,不知下一步该走向哪里。后来想想,不知未来如何是好,那就先把手上的事情...
    茜纯阅读 219评论 0 2