4个值得注意的点(先记后写)
1.加单引号的方法
+ "<p class='addToCarBtn'" + " data-productId='" + productId + "' data-productTitle='" + productTitle + "' data-productCurrentCount='" + productCurrentCount + "' data-productPrice='" + productPrice + "'data-productType='"+productType + "'data-productGdprice='"+productGdprice + "'data-storeProductFreight='"+storeProductFreight + "'data-amount='"+amount + "' onclick='addCar(this);'>加入购物车</p>"
+ "<span class='goods' style='margin-right:10px;'><a " + "data-productId='" + pro[0] + "'data-productPrice='"+pro[3] + "'href='javascript:void(0);' onclick='delProFromCar(this);' style='color:red;'>删除</a></span>" + "<span class='goods'>" + pro[1].substring(0,12) + "</span>" + "<div>" + "<p class='amount'>" + "<i class='i_reduce del' data-productPrice="+ pro[3] +"></i>" //奥义:要明白的一点是,XX+XX在JS里面一定会连起来,所以若写成"<i class='i_reduce del'"+" data-productPrice="+ pro[3] +"></i>"就绝壁会连在一块 + "<input class='goodsAmount' data-productPrice="+ pro[3] +" type='text' value='"+ams+"'>" + "<i class='i_add plus' data-productPrice="+ pro[3] +"></i>" + "</p>" + "<span class='price'>¥<b id='price"+i+"' data-productId=" + pro[0] + " data-productName='" + pro[1] + "' data-productPrice="+ pro[3] +">" + Number(pro[3])*1000*amounts/1000 + "</b></span>"//先乘再除避免出现多位小数的情况 + "</div>" + "</li>";
所以只有两个要注意的,一个是奥义那里,一个是属性的小''写法
又遇到此类问题深入理解一下,例子
popLayout+= "<div class='tip_mask' style="+"height:"+a+'px'+">";
其实属性''的写法不重要,重要的是把“XX+XX在JS里面一定会连起来”理解到位
2.奇怪的on写法
$("#proInfo").on("click",'.plus',function(){ var count = $(this).prev().val(); if(isNaN(count)){ return false; } var amount = $(this).prev().attr("data-productPrice"); var sum = (amount * 100)* (+count+1) / 100; $(this).prev().val(+count+1); var deletebtn=$(this).prev().parent().parent().siblings()[0] var ams=$(deletebtn).children().attr("data-productid"); localStorage.setItem(ams,+count+1); $(this).parent().next().find("b").text(sum); calculatepTotalPrice(); });
3.parent() children()等方法的一个整理
4.this要加$(), 直接给btn加onclick函数的绑定来用this 这种做法的讨论 ,结合2的this方法讨论 ,重写便利店页面,购物车功能的代码(卡在this的地方)