Blogger Information
Blog 18
fans 0
comment 0
visits 11557
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
jQuery基础学习2---五期线上学习班
风行的博客
Original
515 people have browsed it

1遍历

  • parent()父级元素 和parents()所有祖先元素

  • children()直接子元素

  • find()查找后代元素

  • siblings()同胞元素

2获取并设置css类

addclass() 添加类属性

removeclass()删除类属性

hasclass()判断是否含有某个类

text()返回所选元素的文本内容

html()返回html语言

val()获取备选元素的value属性

2实例-购物车

主要是在选择商品以及返回他的状态

实例

<!DOCTYPE html>
<html>
<head>
  <title>模拟加入购物车效果</title>
  <link rel="icon" type="image/x-icon" href="static/images/favicon.ico">
    <script type="text/javascript" src="static/jquery-3.3.1.min.js"></script>
    <style type="text/css">
    * {margin: 0px auto;padding: 0px;}
    .top {width: 402px;height: 35px;line-height: 35px;text-align:center;margin-top: 50px;
          background: #C40000;color:#fff;}
    .main {width: 400px;height: 400px;border: 1px solid #C40000;}
    p {width: 400px;height: 26px;margin-top:10px;}
    b {width: 90px;height: 26px;line-height: 26px;text-align: center;font-size: 12px;color:#838383;
       border: 1px solid #ccc;float: left;margin-left: 5px;}
    span {width: 90px;height: 26px;line-height: 26px;text-align: center;font-size: 12px;color:#838383;
          border: 1px solid #ccc;display: block;float: left;margin-left: 5px;}
    span:hover {cursor: pointer;}
    button {width: 120px;height: 35px;background: #C40000;color: white;border: 0px;}
    button:hover {cursor: pointer;}
    .notice{border:0px;}
    .notice+input{width:60px;margin-left: 6px;text-align: center;}
    .select{border: 2px solid #ff0000;
            width: 88px;height: 24px;
            color: #ff0000;
            line-height: 24px;

    }
    </style>  
</head>
<body>
  <div class="top">请选择信息后加入购物车</div>
  <div class="main">
    <p class="item" name="version">
      <b class="notice">版本</b>
      <span>ONE A2001</span>
      <span>ONE A0001</span>
      <span>ONE A1001</span>
    </p>
    <p class="item" name="color">
      <b class="notice">机身颜色</b>
      <span>白色</span>
      <span>黑色</span>
      <span>金色</span>
    </p>
    <p class="item" name="type">
      <b class="notice">套餐类型</b>
      <span>标配</span>
      <span>套餐一</span>
      <span>套餐二</span>
    </p>
    <p class="item" name="ram">
      <b class="notice">运行内存</b>
      <span>2GB</span>
      <span>3GB</span>
      <span>4GB</span>
    </p>
    <p class="item" name="rom">
      <b class="notice">机身内存</b>
      <span>16GB</span>
      <span>32GB</span>
      <span>64GB</span>
    </p>
    <p class="item" name="location">
      <b class="notice">产地</b>
      <span>中国大陆</span>
      <span>港澳台</span>
    </p>
    <p class="item" name="price">
      <b class="notice">价格</b>
      <span>999元抢购</span>
    </p>
    <p class="item1" name="num">
      <b class="notice">数量</b>
      <input type="number" value="1" >
    </p>

    <p style="margin-top:30px;margin-left:95px;">
      <button class="bu1" id='sub'>加入购物车</button>
    </p>
  </div>

<script>
  $(function(){
    $('span').click(function(){
      if($(this).hasClass('select')){  //是否被选中
           $(this).removeClass('select');
      }else{
           $(this).addClass('select').siblings('span').removeClass('select');
       }
    })
    
    $('#sub').click(function(){
      var form={}; //创建一个对象
      var flag=true; //判断能不能加入购物车
      $('.item').each(function(){
        if($(this).children('span.select').length!=1){
          flag=false;
        }else{
          var key=$(this).attr('name'); //p标签 name的属性值
          var value=$(this).children('span.select').html();
          form[key]=value; //数组键值对一一对应
        }
      })
      
      if($('.item1 input').val()<=0){
        flag=false;
      }else{
        form['num']=$('.item1 input').val();
      }
      
      if(flag){
        alert('加入购物车!');
      }
    })

  })
</script>

</body>
</html>

运行实例 »

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


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post