JavaScript 常见问题及解决方法
(1)数值相加时结尾自动多出“0”
解决方法:
- var totalPriceStr=String(totalPrice);
- / if(totalPriceStr)
- var find = "\\.";//对句点要转义
- var reg = new RegExp(find, "g");
- var c = totalPriceStr.match(reg);
- if(c){
- if(com.whuang.hsj.endWith(totalPriceStr,'00004')){
- alert("111");
- totalPriceStr=totalPriceStr.replace(/[0]+0004$/,"");
- }
- }
- newTd03.innerHTML ="总计:"+totalPriceStr+"元";
说明:totalPrice是Number类型,值为:44.230000000000004
(2)判断字符串是否包含句点
- var find = "\\.";//对句点要转义
- var reg = new RegExp(find, "g");
- var c = totalPriceStr.match(reg);
- if(c){
- alert("has .")
- }else{
- alert("no . !!!") ;
- }
(3)如何清空数组
- var shoppingCart=new Array();//保存购物车内容
- shoppingCart.splice(0,shoppingCart.length);
(4) 使用jQuery 清空div
- var shoppingCartDiv=$('#shoppingCartDiv');
- hoppingCartDiv.empty();