js day14 案例01

1.改变body的背景颜色

<body id='body'>
    <div id='container'>
          <h1>鹅</h1>
        <p>鹅,鹅,鹅,</p>
        <p>取勃用刀割</p>
        <p>拔毛烧开水</p>
        <p>铁锅炖大鹅</p>
    </div>
    <script type="text/javascript">
        var container=document.getElementById('container');
        container.onmouseover=function(){
            body.style.backgroundColor='#f00';
            body.style.color='#0f0';
        }

        container.onmouseout=function(){
            body.style.backgroundColor='#ff0';
            body.style.color='#f00';
        }
    </script>
</body>

2.导航条

<style type="text/css">
            *{
                margin:0;
                padding:0;
                box-sizing: border-box;
            }
            li{
                list-style: none;
            }
            a{
                text-decoration: none;
            }
            ul{
                width:500px;
                margin:0 auto;
                height:40px;
                line-height: 40px;
                background: #e4393c;
            }
            ul>li{
                float:left;
            }
            ul>li>a{
                display: inline-block;
                width:100px;
                text-align: center;
                color:#fff;
            }
</style>
    <body>
         <ul>
            <li>
                <a href="#">首页</a>
            </li>
            <li>
                <a href="#">公司介绍</a>
            </li>
            <li>
                <a href="#">联系我们</a>
            </li>
            <li>
                <a href="#">涉及分类</a>
            </li>
            <li>
                <a href="#">综合设计</a>
            </li>
         </ul>
         <script type="text/javascript">
            var li=document.querySelectorAll('ul>li');
            console.log(li);
            for(var i=0;i<li.length;i++){
                li[i].onmouseover=function(){
                    this.style.background='#fff';
                    this.firstElementChild.style.color='red';
                }
                li[i].onmouseout=function(){
                    this.style.background='#e4393c';
                    this.firstElementChild.style.color='#fff';
                }
            }
         </script>
    </body>

3.购物车

tr表示行

td表示列 th可以加粗显示

<table border="1" cellspacing="0" width="500">
    <thead>
    <tr>
        <th>商品名称</th>
        <th>单价</th>
        <th>数量</th>
        <th>小计</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>iphone</td>
        <td>&yen;5999</td>
        <td>
            <button onclick="calc(this)">+</button>
            <span>1</span>
            <button onclick="calc(this)">-</button>
        </td>
        <td>&yen;5999</td>
    </tr>
    <tr>
        <td>iphone</td>
        <td>&yen;5999</td>
        <td>
            <button onclick="calc(this)">+</button>
            <span>1</span>
            <button onclick="calc(this)">-</button>
        </td>
        <td>&yen;5999</td>
    </tr>
    <tr>
        <td>iphone</td>
        <td>&yen;5999</td>
        <td>
            <button onclick="calc(this)">+</button>
            <span>1</span>
            <button onclick="calc(this)">-</button>
        </td>
        <td>&yen;5999</td>
    </tr>
    </tbody>
    <tfoot>
    <tr>
        <td colspan="3">总计</td>
        <td>&yen;17997</td>
    </tr>     
    </tfoot>
</table>
<script>

跨列colspan

跨行rowspan

function calc(btn){
//让n加1或减1通过btn的父元素找到span元素
var span=btn.parentElement
        .querySelector("span");
//保存在变量span中获取span的内容保存在变量n中
var n=parseInt(span.innerHTML);
    console.log(typeof(n));
//如果btn的内容是"+",就让n+1否则如果n>1,就n--,否则n=1
if(btn.innerHTML=="+"){
    n++;
}else if(n>1){
    n--
}else{
    n=1
}

设置span的内容为n

span.innerHTML=n;

第二步让每行的小计变化

找到btn的父元素的前一个兄弟元素的内容

并截取出数字保存在变量price中

var price=btn.parentElement
        .previousElementSibling
        .innerHTML
        .slice(1);
//声明subtotal表示小计,subtotal=price*n
var subtotal=price*n;
//找到btn的父元素的下一个兄弟元素并设置
// 内容为"&yen;"+subtotal.toFixed(2)
btn.parentElement
        .nextElementSibling
.innerHTML="&yen;"+subtotal
        .toFixed(2);

计算总价

获取tbody中每行最后一个td保存在tds中

 var tds=document.querySelectorAll('tbody>tr>td:last-child');
    console.log(tds);
for(var i=0,total=0;i<tds.length;i++){
    total+=parseFloat(tds[i].innerHTML.slice(1));
        
}
    
 document.querySelector('tfoot>tr>td:last-child').innerHTML='&yen;'+total.toFixed(2);   
   
}
</script>
</body>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • HTML 5 HTML5概述 因特网上的信息是以网页的形式展示给用户的,因此网页是网络信息传递的载体。网页文件是用...
    阿啊阿吖丁阅读 4,173评论 0 0
  • DOM元素、改变背景颜色、导航条、购物车、万年历、图片切换、树形列表 2018-05-26 09:09 · 字数 ...
    _z王箭阅读 280评论 0 1
  • 本性难移(5) 这么说,我还是没能杀了她。 我的眼皮很沉,似乎压着铅块,怎么也抬不起来。头不敢动,一动就天旋地转。...
    蒋薰然阅读 594评论 0 0
  • 今 夜 闲 庭 信 步, 一 路 烟 雨 幕 幕。 常 念 心 何 住, 清 泪 两 行 处。 雷 鼓 , 雷 鼓...
    胖艺阅读 697评论 0 2
  • HTML写的页面是静态页面,超文本页面。 PHP写的页面是动态页面,可以交互,动态输出信息。
    BugLuv阅读 1,187评论 0 0