博客列表 >相册与计算器——3月29日

相册与计算器——3月29日

勇闯天涯不喝酒的博客
勇闯天涯不喝酒的博客原创
2018年04月02日 17:06:28666浏览

相册:

1.学习js代码语法调用和函数。

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>明星相册</title>
    <style type="text/css">
        .box{
            width: 550px;
            height: 700px;
            background-color: #fbfbfb;
            border: 1px solid #777777;
            margin: 20px auto;
            text-align: center;
            box-shadow: 2px 2px 2px #999;
            color:#888888;
        }
        .box ul{
            margin: 0;
            padding: 0;
            /*将ul转为BFC独立块,使之不受内部浮动元素的影响*/
            overflow: hidden;
        }
        .box ul li {
            list-style-type: none;
            float: left;
            background-color: skyblue;
            margin-left: 28px;
        }
        .box ul li a{
            /*将a标签设置为块元素,就可以设置宽高,使整个块元素可以被点击*/
            display: block;
            width: 100px;
            height: 40px;
            line-height:40px;
            color: white;
            text-decoration: none;
        }
        /*:hover 选择器用于选择鼠标指针浮动在上面的元素*/
        .box ul li a:hover{
            font-size: 1.1em;
            color: blanchedalmond;
        }
        .box .pic{
            width: 420px;
            height: 500px;
            /*消除底部微小空白区*/
            line-height: 1px;
            border: 1px solid #363636;
            margin: 50px auto auto auto;
        }
        .box .pic img{
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
<!--知识要点:js代码写在哪里? 怎么调用? 函数的用法?-->
 <div class="box">
     <h2>明星相册</h2>
     <ul>
         <li>
             <a href="images/fbb.jpg" title="《还珠格格》,《武媚粮传奇》,《我不是潘金莲》..." onclick="changePic(this);return false">范冰冰</a>
         </li>
         <li>
             <a href="images/gyy.jpg" title="《倚天屠龙记》,《咱们结婚吧》,《爱无悔》..." onclick="changePic(this);return false">高圆圆</a>
         </li>
         <li>
             <a href="images/sl.jpg" title="《甄嬛传》,《那年花开月正圆》,《芈月传》..." onclick="changePic(this);return false">高圆圆</a>
         </li>
         <li>
             <a href="images/zly.jpg" title="《楚乔传》,《花千骨》,《陆贞传奇》..." onclick="changePic(this);return false">赵丽颖</a>
         </li>
     </ul>
     <div class="pic">
         <img src="images/zwt.png" alt="" id="img">
     </div>
     <p id="info"></p>
 </div>
    <script type="text/javascript">
        function changePic(pic){
    //        通过函数获取简介 名字 url把它们装在容器里(用.方法)p
            var picInfo = pic.title;
            var picName = pic.innerHTML;
            var picUrl = pic.href;
    //        将页面中要被替换的元素装在容器里(decument对象的方式)
            var img = document.getElementById('img');
            var p = document.getElementById('info');
    //        替换对象
            img.src = picUrl;
            p.innerHTML = picName+':'+picInfo;
            p.innerHTML = '<span style="color:coral">'+picName+':'+picInfo+'</span>';
        }
    </script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

简易计算器:

  1. 知识点:获取页面元素  判断语句 事件方法函数的使用


  2. 实例

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>简易计算器</title>
        <style type="text/css">
            .box{
                width: 500px;
                height: 200px;
                background-color:lightgray;
                border: 1px solid #999999;
                border-radius:15px;
                box-shadow: 2px 2px 1px #888;
                margin:50px auto;
                color: #505050;
                text-align: center;
            }
            table{
                margin: auto;
            }
            td{
                width: 100px;
                height: 30px;
                padding: 5px 10px;
            }
            input,select{
                width: 100%;
                height: 100%;
                border: none;
                text-align: center;
    
            }
            button{
                width: 100%;
                height: 100%;
                background-color: skyblue;
                color: white;
            }
            button:hover{
                cursor:pointer;
                width: 105%;
                height: 105%;
                background-color: crimson;
            }
        </style>
    </head>
    <body>
    <!--知识点:获取页面元素  判断语句 事件方法函数的使用-->
        <div class="box">
            <h2>简易计算器</h2>
            <form>
                <table>
                    <tr>
                        <td><input type="text" name="opt1" placeholder="操作数1"></td>
                        <td>
                            <select name="option">
                                <option value="null">请选择操作</option>
                                <option value="add">+</option>
                                <option value="sub">-</option>
                                <option value="mul">*</option>
                                <option value="div">/</option>
                            </select>
                        </td>
                        <td><input type="text" name="opt2" placeholder="操作数2"></td>
                        <td><button type="button">计算</button></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right"><h3>结果:</h3></td>
                        <td colspan="2" align="left"><h3 id="placeholder"></h3></td>
                    </tr>
                </table>
            </form>
        </div>
        <script type="text/javascript">
    //        获取操作数 按钮与占位符
            var opt1 = document.getElementsByName('opt1')[0];
            var opt2 = document.getElementsByName('opt2')[0];
            var opt = document.getElementsByName('option')[0];
            var btt = document.getElementsByTagName('button')[0];
            var placeholder = document.getElementById('placeholder');
    
    //        给按钮添加事件并计算
            btt.onclick = function() {
                if (opt1.value.length == 0){
                    alert("第一个操作数不能为空");
                    opt1.focus();
                    return false;
                }
                else if (isNaN(opt1.value)){
                    alert("非法操作:第一个数必须为数字");
                    opt1.focus();
                    return false
                }
                else if (opt2.value.length == 0){
                    alert("第二个操作数不能为空");
                    opt2.focus();
                    return false;
                }
                else if (isNaN(opt2.value)){
                    alert("非法操作:第二个数必须为数字");
                    opt2.focus();
                    return false;
                }
                else{
                    var data1 = parseFloat(opt1.value);
                    var data2 = parseFloat(opt2.value);
                }
    
                var option = opt.value;
                var temp = 0;
                var flag = '';
                var result = '';
                switch(option){
                    case 'null':
                        alert("请选择操作类型");
                        opt.focus();
                        return false;
                    case 'add':
                        flag = '+';
                        temp = data1 + data2;
                        break;
                    case 'sub':
                        flag = '-';
                        temp = data1 - data2;
                        break;
                    case 'mul':
                        flag = '*';
                        temp = data1 *data2;
                        break;
                    case 'div':
                        flag = '/';
                        if(data2 == 0){
                            alert('除数不能为0,请重新输入');
                            opt2.focus();
                            return false
                        }else{
                            temp = data1 / data2;
                        }
                        break
                }
            placeholder.innerHTML ='<span style="color:coral">' + data1 + flag + data2 + '=' + temp + '</span>'
            }
        </script>
    </body>
    </html>

    运行实例 »

    点击 "运行实例" 按钮查看在线实例

    手抄代码示例:

    3001.png3002.png3003.png3004.png3005.png3006.png

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议