JS 카 트 기본 기능 구현

22922 단어 JS쇼핑 카 트
JS 는 카 트 상품 의 추가,감소,단일 선택,전체 선택,삭제,수 동 입력,가격 업데이트 등 기능 을 실현 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

자 바스 크 립 트 코드

$(function(){
 
 $("#footer").hover(function(){
 $("#footer").css({"border":"#e00"})
 },)
 
 var inputs=document.getElementsByName("good-id");
 var all=document.getElementsByName("all")[0];
 
 all.onclick=function(){
 for(var i=0;i<inputs.length;i++){
 inputs[i].checked=this.checked;
 }
 sumprice();
 }
 
 for(var i=0;i<inputs.length;i++){
 inputs[i].onclick=function(){
 var count=0;
 for(var j=0;j<inputs.length;j++){
 if(inputs[j].checked==true){
 count++
 }
 }
 if(count<inputs.length){
 all.checked=false;
 }else{
 all.checked=true;
 }
 sumprice();
 }
 }
 
 //  
 var minus=document.getElementsByName("minus");
 for(var i=0;i<minus.length;i++){
 minus[i].onclick=function(){
 var counts=this.nextElementSibling;
 var count=parseInt(counts.value);
 if(count>1){
 counts.value=--count;
 }
 sumprice();
 }
 }
 //  
 var plus=document.getElementsByName("plus");
 for(var i=0;i<plus.length;i++){
 plus[i].onclick=function(){
 var counts=this.previousElementSibling;
 var count=parseInt(counts.value);
 counts.value=++count;
 sumprice();
 }
 }
 
 //    
 var counts=document.getElementsByName("count");
 for(var i=0;i<counts.length;i++){
 counts[i].onblur=function(){
 var count=parseInt(this.value);
 if(isNaN(count)||count<1){
 count=1;
 }
 this.value=count;
 sumprice();
 }
 }
 
 
 //  
function sumprice() {
 var tbody = document.getElementById("cart-goods-list");
 var tbodyTr = tbody.getElementsByTagName("tr");
 var sumprice=0;
 for(var i = 0; i < tbodyTr.length; i++) {
 //    
 var priceEm = tbodyTr[i].getElementsByClassName("price-em")[0];
 var price = parseFloat(priceEm.innerText);

 //    
 var counts = tbodyTr[i].getElementsByClassName("combo-value")[0];
 var count = parseInt(counts.value);
 //  
 var chengji=price*count;
 //         
 var amountEm=tbodyTr[i].getElementsByClassName("amount-em")[0];
 amountEm.innerText=chengji.toFixed(2);
 
 //     
 var liD=tbodyTr[i].getElementsByTagName("input")[0];
 if(liD.checked){
 sumprice+=chengji;
 }
 }
 var zong=document.getElementById("total-amount-em");
 zong.innerText=sumprice.toFixed(2);
}
 
 //  
 document.getElementById("cart-delete").onclick=function(){
 var tbody = document.getElementById("cart-goods-list");
 var del=[];
 for(var i=0;i<inputs.length;i++){
 if(inputs[i].checked){
 del.push(inputs[i].parentElement.parentElement);
 }
 }
 for(var i=0;i<del.length;i++){
 tbody.removeChild(del[i]);
 }
 all.checked=false;
 sumprice();
 }
 
 document.getElementById("total-amount").onmouseover=function(){
 document.getElementById("total-amount").style.cursor="pointer";
 }
 
 document.getElementById("total-amount").onclick=function(){
 var tbody = document.getElementById("cart-goods-list");
 var tr=document.createElement("tr");
 tr.innerHTML='<tr><td><input type="checkbox" name="good-id" value="1"></td><td class="goods"><div class="goods-image"><img src="./pic/goods/1.jpg"></div><div class="goods-information"><h3>Dior                </h3><ul><li>50  </li><li>   7      </li></ul></div></td><td><span class="price">¥<em class="price-em">498.00</em></span></td><td><div class="combo"><input type="button" name="minus" value="-" class="combo-minus"><input type="text" name="count" value="1" class="combo-value"><input type="button" name="plus" value="+" class="combo-plus"></div></td><td><strong class="amount">¥<em class="amount-em">498.00</em></strong></td></tr>';
 tbody.appendChild(tr);
 }
})
CSS 코드

@charset "utf-8";

#main{
 padding: 30px 0px;
}

#cart{
 background: #FFFFFF;
 padding: 40px;
}

#cart h1{
 line-height: 40px;
 padding: 0px 0px 10px 0px;
}

table.form{
 border-collapse: collapse;
 empty-cells: show;
 margin: 20px 0px;
 padding: 0px;
 table-layout: fixed;
 width: 100%;
}

table.form th,
table.form td{
 border-bottom: 1px solid #DDDDDD;
 padding: 15px 10px;
 text-align: left;
}

table.form{
 border-top: 3px solid #DDDDDD;
}

.goods .goods-image img{
 border: 1px solid #DDDDDD;
 float: left;
 height: 100px;
 margin: 0px 20px 0px 0px;
}

.goods .goods-information{
 float: left;
}

.goods .goods-information ul{
 color: #666666;
 font-size: 12px;
 line-height: 20px;
 margin:10px 0px 0px 0px;
}

.price,
.amount,
#total-amount{
 color: #E00000;
}

#total-amount{
 font-size: 22px;
}

.price em,
.amount em,
#total-amount em{
 font-style: normal;
}

.combo .combo-minus,
.combo .combo-value,
.combo .combo-plus{
 background: #FFFFFF;
 border: 1px solid #DDDDDD;
 color: #333333;
 float: left;
 font-weight: bold;
 margin: 0px;
 outline: none;
 text-align: center;
}

.combo .combo-minus,
.combo .combo-plus{
 font-size: 16px;
 height: 26px;
 line-height: 26px;
 padding: 0px;
 width: 24px;
}

.combo .combo-value{
 border-left: none;
 border-right: none;
 height: 20px;
 line-height: 20px;
 padding: 2px;
 width: 40px;
}

#cart-delete{
 margin-left: 20px;
}

#settlement{
 background: #E00000;
 border: none;
 color: #FFFFFF;
 float: right;
 font-size: 16px;
 height: 40px;
 line-height: 40px;
 margin: 0px;
 outline: none;
 padding: 0px;
 width: 160px;
}
HTML 코드

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>  </title>
 <link rel="stylesheet" href="./css/common.css" rel="external nofollow" >
 <link rel="stylesheet" href="./iconfont/iconfont.css" rel="external nofollow" >
 <link rel="stylesheet" href="./css/cart.css" rel="external nofollow" >
 <script type="text/javascript" src="js/jquery-3.1.1.js"></script>
 <script type="text/javascript" src="./js/cart2.js"></script>
 </head>
 <body>
 <div id="topbar">
 <div class="container">
 <div id="topbar-location">
 <i class="iconfont icon-location"></i>
 <a href="#">  </a>
 </div>
 <div id="topbar-menu">
 <ul>
 <li>
 <a href="#"class="red">  </a>
 </li>
 <li class="separator">|</li>
 <li>
 <a href="./register.html" rel="external nofollow" >    </a>
 </li>
 <li class="separator">|</li>
 <li>
 <a href="#">    </a>
 </li>
 <li class="separator">|</li>
 <li>
 <a href="#">   </a>
 </li>
 <li class="separator">|</li>
 <li>
 <a href="#">    </a>
 </li>
 </ul>
 </div>
 </div>
 </div>
 <div id="header">
 <div class="container">
 <div id="header-logo">
 <a href="./index.html" rel="external nofollow" >
 <h1>LOGO</h1>
 </a>
 </div>
 <div id="header-search">
 <div id="header-search-combobox">
 <form action="#" method="post">
 <input type="text" name="keywords" placeholder="  " id="header-search-combobox-text">
 <button type="submit" id="header-search-combobox-button">
 <i class="iconfont icon-search"></i>
 </button>
 </form>
 </div>
 <div class="clear">
 <ul id="header-search-hotkeywords">
 <li>
 <strong>  :</strong>
 </li>
 <li>
 <a href="#">   </a>
 </li>
 <li class="separator">|</li>
 <li>
 <a href="#">  </a>
 </li>
 <li class="separator">|</li>
 <li>
 <a href="#">  </a>
 </li>
 <li class="separator">|</li>
 <li>
 <a href="#">  </a>
 </li>
 </ul>
 <a href="#"id="header-search-advancedsearch">    </a>
 </div>
 </div>
 <div id="header-cart">
 <i class="iconfont icon-cart"></i>
 <a href="#">   </a>
 <strong>0</strong>
 </div>
 </div>
 </div>
 <div id="navigation">
 <div class="container">
 <div id="navigation-category">
 <div id="navigation-category-heading">
 <div class="float-left">
 <i class="iconfont icon-menu"></i>
 <strong>      </strong>
 </div>
 <div class="float-right">
 <i class="iconfont icon-dropdown"></i>
 </div>
 </div>
 <div id="navigation-category-content">
 <dl>
 <dt>    </dt>
 <dd>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 </dd>
 </dl>
 <dl>
 <dt>    </dt>
 <dd>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 </dd>
 </dl>
 <dl>
 <dt>    </dt>
 <dd>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 </dd>
 </dl>
 <dl>
 <dt>    </dt>
 <dd>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 </dd>
 </dl>
 <dl>
 <dt>    </dt>
 <dd>
 <a href="#">    </a>
 <em>|</em>
 <a href="#">   </a>
 <em>|</em>
 <a href="#">    </a>
 <em>|</em>
 <a href="#">    </a>
 <em>|</em>
 <a href="#">    </a>
 </dd>
 </dl>
 <dl>
 <dt>    </dt>
 <dd>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 <em>|</em>
 <a href="#">  </a>
 </dd>
 </dl>
 </div>
 </div>
 <div id="navigation-menu">
 <ul>
 <li>
 <a href="#">  </a>
 </li>
 <li>
 <a href="#">  </a>
 </li>
 <li>
 <a href="#">   </a>
 </li>
 </ul>
 </div>
 </div>
 </div>
 <div id="main">
 <div class="container">
 <div id="cart">
 <h1>   </h1>
 <form action="#" method="post">
 <table class="form">
 <thead>
 <tr>
 <th width="8%">  </th>
 <th width="50%">  </th>
 <th width="13%">  ( )</th>
 <th width="15%">  </th>
 <th width="14%">  ( )</th>
 </tr>
 </thead>
 <tbody id="cart-goods-list">
 <tr>
 <td>
 <input type="checkbox" name="good-id" value="1">
 </td>
 <td class="goods">
 <div class="goods-image">
 <img src="./pic/goods/1.jpg">
 </div>
 <div class="goods-information">
 <h3>Dior                </h3>
 <ul>
 <li>50  </li>
 <li>   7      </li>
 </ul>
 </div>
 </td>
 <td>
 <span class="price">¥<em class="price-em">498.00</em></span>
 </td>
 <td>
 <div class="combo">
 <input type="button" name="minus" value="-" class="combo-minus">
 <input type="text" name="count" value="1" class="combo-value">
 <input type="button" name="plus" value="+" class="combo-plus">
 </div>
 </td>
 <td>
 <strong class="amount">¥<em class="amount-em">498.00</em></strong>
 </td>
 </tr>
 <tr>
 <td>
 <input type="checkbox" name="good-id" value="2">
 </td>
 <td class="goods">
 <div class="goods-image">
 <img src="./pic/goods/3.jpg">
 </div>
 <div class="goods-information">
 <h3>LANCÔME             </h3>
 <ul>
 <li>50  </li>
 <li>   7      </li>
 </ul>
 </div>
 </td>
 <td>
 <span class="price">¥<em class="price-em">598.00</em></span>
 </td>
 <td>
 <div class="combo">
 <input type="button" name="minus" value="-" class="combo-minus">
 <input type="text" name="count" value="1" class="combo-value">
 <input type="button" name="plus" value="+" class="combo-plus">
 </div>
 </td>
 <td>
 <strong class="amount">¥<em class="amount-em">598.00</em></strong>
 </td>
 </tr>
 </tbody>
 <tfoot>
 <tr>
 <td colspan="2">
 <label>
 <input type="checkbox" name="all">
 <span>  </span>
 </label>
 <a href="#"id="cart-delete">  </a>
 </td>
 <td colspan="3">
 <span>  :</span>
 <strong id="total-amount">¥<em id="total-amount-em">0.00</em></strong>
 <input type="submit" value="    " id="settlement">
 </td>
 </tr>
 </tfoot>

 </table>
 </form>
 </div>
 </div>
 </div>
 <div id="footer">
 <div id="footer-promise">
 <div class="container clear">
 <div class="footer-promise-column clear">
 <img src="./img/1.png">
 <dl>
 <dt>100%  </dt>
 <dd>         </dd>
 </dl>
 </div>
 <div class="footer-promise-column clear">
 <img src="./img/2.png">
 <dl>
 <dt>    </dt>
 <dd>         </dd>
 </dl>
 </div>
 <div class="footer-promise-column clear">
 <img src="./img/3.png">
 <dl>
 <dt>    </dt>
 <dd>         </dd>
 </dl>
 </div>
 <div class="footer-promise-column clear">
 <img src="./img/4.png">
 <dl>
 <dt>    </dt>
 <dd>         </dd>
 </dl>
 </div>
 </div>
 </div>
 <div id="footer-link">
 <div class="container clear">
 <div class="footer-link-column">
 <h3>    </h3>
 <ul>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 </ul>
 </div>
 <div class="footer-link-column">
 <h3>    </h3>
 <ul>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >     </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 </ul>
 </div>
 <div class="footer-link-column">
 <h3>    </h3>
 <ul>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 </ul>
 </div>
 <div class="footer-link-column">
 <h3>    </h3>
 <ul>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 </ul>
 </div>
 <div class="footer-link-column">
 <h3>    </h3>
 <ul>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >    </a>
 </li>
 <li>
 <a href="#" >     </a>
 </li>
 </ul>
 </div>
 </div>
 </div>
 <div id="footer-menu">
 <ul>
 <li>
 <a href="#" >    </a>
 </li>
 <li class="separator">|</li>
 <li>
 <a href="#" >    </a>
 </li>
 <li class="separator">|</li>
 <li>
 <a href="#" >    </a>
 </li>
 <li class="separator">|</li>
 <li>
 <a href="#" >    </a>
 </li>
 <li class="separator">|</li>
 <li>
 <a href="#" >    </a>
 </li>
 </ul>
 </div>
 <div id="footer-copyright">
 <p>Copyright &copy; 2016 XXX. All Rights Reserved.</p>
 </div>
 </div>
 </body>

</html>
주:CSS 스타일 코드 가 너무 많아 서 위 에서 다 주지 않 고 주요 코드 만 주 었 습 니 다.젊은이 들 은 실제 상황 에 따라 스타일 을 수정 할 수 있다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기