手机端点击d窗处的“加入购物车”后,”购物车“处的数字变化怎么在html写

手机端点击d窗处的“加入购物车”后,”购物车“处的数字变化怎么在html写,第1张

就是简单的数字加减,点击加入购物车后,将购物车的数字内容提取出来加一再赋值到内容里。一般数字部分会单独给一个标签来写入变量给js进行 *** 作,

购物车还有加减按钮,也是将标签内容的数字加一减一就行了

数字的改变需要用到 js 或者其他js框架来进行 *** 作

<html xmlns="http://www.w3.org/1999/xhtml">    

<head>    

<meta http-equiv="Content-Type" content="text/html charset=gb2312" />    

<title>修改订单</title>    

<style type="text/css">    

body{    

font-size:13px    

line-height:25px    

}    

table{    

border-top: 1px solid #333    

border-left: 1px solid #333    

width:400px    

}    

td{    

border-right: 1px solid #333    

border-bottom: 1px solid #333    

text-align:center    

}    

.title{

    

font-weight:bold    

background-color: #cccccc    

}    

input text{    

width:100px    

}    

         

</style>    

<script type="text/javascript">    

function addRow(){    

//行的长度    

var rowlength=document.getElementById("order").rows.length    

//得到整个表格对象    

var order = document.getElementById("order").insertRow(rowlength-1)    

order.id=rowlength-1    

//插入列    

var cel1=order.insertCell(0).innerHTML="游戏光盘"    

var cel2=order.insertCell(1).innerHTML="34"    

var cel3=order.insertCell(2).innerHTML="&yen58.40"    

var cel4=order.insertCell(3).innerHTML="<input type=\"button\"value=\"删除\" onclick=\"delRow('"+(rowlength-1)+"')\"/>"+ "<input type=\"button\"value=\"修改\" onclick=\"editRow('"+(rowlength-1)+"')\"/>"    

}    

function delRow(qwe){    

var ewq=document.getElementById(qwe).rowIndex    

document.getElementById("order").deleteRow(ewq)    

}    

function editRow(rowID){    

var row=document.getElementById(rowID)    

var cel=row.cells    

var text=cel[1].innerHTML    

cel[1].innerHTML="<input type='text' value='"+text+"' style='width:40px'>"    

cel[3].lastChild.value="确定"    

cel[3].lastChild.setAttribute("onclick","update('"+rowID+"')")    

}    

    

function update(qwe){    

var row=document.getElementById(qwe)    

var cel=row.cells    

var text=cel[1].lastChild.value    

cel[1].innerHTML=text    

cel[3].lastChild.value="修改"    

cel[3].lastChild.setAttribute("onclick","editRow('"+qwe+"')")    

}    

/*

    

function add(){    

var a = document.getElementById("order").rows.length    

var b = document.getElementById("order").insertRow(a-1)    

var one1 = b.insertCell(0).innerHTML="123"    

}    

*/    

</script>    

</head>    

<body>    

<table border="0" cellspacing="0" cellpadding="0" id="order">    

 <tr class="title">    

   <td>商品名称</td>    

   <td>数量</td>    

   <td>价格</td>    

   <td> *** 作</td>    

 </tr>    

 <tr id="1">    

   <td>防滑真皮休闲鞋</td>    

   <td>12</td>    

   <td>&yen568.50</td>    

   <td><input name="rowdel" type="button" value="删除" onclick='delRow("1")' />    

   <input id="edit1" type="button" value="修改" onclick='editRow("1")' /></td>    

 </tr>    

 <tr>    

   <td colspan="4" style="height:30px">    

   <input name="addOrder" type="button" value="增加订单" onclick="addRow()" /></td>    

 </tr>    

</table>    

</body>    

</html>

这个是我原来上学的时候练习的代码,练习的是基础的jsDOM *** 作,不过建议以后用Jquery 比较方便  有什么不懂得可以问我

以下是一个简易购物车的HTML+CSS和jQuery代码示例:

HTML部分:

<div class="cart-container">

<h2>购物车</h2>

<ul class="cart-items">

<li class="cart-item">

<img src="item1.jpg" alt="商品1">

<span class="item-name">商品1</span>

<span class="item-price">100元</span>

<input type="number" class="item-quantity" value="1">

<button class="remove-btn">删除</button>

</li>

<li class="cart-item">

<img src="item2.jpg" alt="商品2">

<span class="item-name">商品2</span>

<span class="item-price">200元</span>

<input type="number" class="item-quantity" value="1">

<button class="remove-btn">删除</button>

</li>

</ul>

<p class="total-price">总价:<span>300元</span></p>

</div>

CSS部分:

.cart-container {

width: 400px

border: 1px solid #ccc

padding: 20px

}

.cart-items {

list-style-type: none

padding: 0

margin: 0

}

.cart-item {

display: flex

align-items: center

margin-bottom: 10px

}

.cart-item img {

width: 80px

height: 80px

margin-right: 10px

}

.item-name, .item-price {

flex: 1

}

.item-quantity {

width: 50px

margin-right: 10px

}

.remove-btn {

background-color: #ccc

border: none

padding: 5px 10px

cursor: pointer

}

.total-price {

margin-top: 20px

text-align: right

}

jQuery部分:

$(document).ready(function() {

// 计算初始总价

updateTotalPrice()

// 删除商品按钮点击事件

$('.remove-btn').click(function() {

$(this).parent().remove()

updateTotalPrice()

})

// 商品数量输入框变化事件

$('.item-quantity').change(function() {

updateTotalPrice()

})

// 更新总价函数

function updateTotalPrice() {

var total = 0

$('.cart-item').each(function() {

var price = parseInt($(this).find('.item-price').text())

var quantity = parseInt($(this).find('.item-quantity').val())

total += price * quantity

})

$('.total-price span').text(total + '元')

}

})

该示例中,使用了HTML和CSS来构建购物车的界面,使用jQuery来实现购物车的逻辑。购物车中的每个商品都包含了商品名称、商品图片、商品价格、商品数量和删除按钮,当点击删除按钮时,对应的商品会从购物车中删除并重新计算总价;当商品数量变化时,总价也会自动更新。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11897728.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存