1.each的用法:
如果选择器选择到的对象有多个,那么需要用each包裹一层来执行,否则,无法达到想要的效果。
// 取消
$(".sale-reset").click(function(){
$(this).parents(".jarea").hide();
$(this).parents(".jarea").find("input:checked").each(function(){
$(this).removeAttr("checked");
});
$(".sale-check-num").each(function(){$(this).html('')});
});
$(".child").find("input:checked").each(function(){
alert($(this).val());
});
2.筛选:
<li class="selected-content-item" title="狼人女是衬衫" productcode="120856490" style="display: list-item;">
<p class="goods-operate">
<i class='tip tip-ok tip-mini'></i><span>已保存</span> <a href="javascript:;" class="right">编辑</a>
</p>
</li>
不包含于 ul下的li不包含有i[class='tip tip-ok tip-mini']
$("ul.selected-content-list li:not(:has(i[class='tip tip-ok tip-mini']))").length
3.checkbox是否被勾选中的状态判断:
checkbox被选中有两种表现方式:1. checked="true" 2. checked="checked"
所以,判断它的选中状态就比较麻烦。建议使用.checked属性,返回true或者false
但是,.checked属性必须是dom对象不是jquery对象,那么怎么办呢。
if($("#testCheckBox")[0].checked ){.......} 这样就把jquery对象转化为dom对象然后来判断checkbox是否被选中了。
另:
滚动条
sign.fixedTop = function(o,p) {
$(window).scroll(function() {
var _h = $(".pick-goods-full").offset().top + 156;
var _sh = $(document.documentElement).scrollTop() || $(document.body).scrollTop();
打印日志
console.log(_h,_sh)
if (_sh >= _h) {
o.css({'position':'fixed','top':0,'marginRight':p+'px'});
o.next().css({"marginTop":'107px'})
} else {
o.css({'position':'static','marginRight':0})
o.next().css({"marginTop":'0'})
}
})
}
sign.fixedTop($(".left-selected-content"));
sign.fixedTop($(".right-goods-detail"),25);
4.JS 中对象类似使用:new Object();
$("#test").click(function(){
var obj = new Object();
obj["a"] = "1";
obj.a = "2";
console.log(obj.a);
console.log(obj["a"]);
console.log(obj);
//delete(obj["a"]);delete(obj.a);//删除对象的属性
});
打印:2,2 , Object {a: "2"}
从现象看,不管是[]还是.只要名字一样,维护的都是一个属性。
注意:[]的时候要“”号,否则报错。反之.的时候不需要。
作用:.的就是属性,要用的时候需要明确的定义属性名。[]的作用类似于List,需要一个集合存储数据时不知道明确的名称(比如存的只是商家编码)时使用。
例:var code = selectedProductPageList[currentProdutCode];
if (currentProdutCode == code){};//判断当前点击的productCode 是不是在selectedProductPageList集合里
扩展:当使用Object()当做list用时,无法直接根据length判断长度,需要 var length = 0; for(var o in obj){length++};这样来看长度。
5 JS 中数组使用:
Array.prototype.indexOfQua = function(val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val)
return i;
}
return -1;
};
Array.prototype.removeQua = function(val) {
var index = this.indexOfQua(val);
if (index > -1) {
this.splice(index, 1);
}
};
Array.prototype.contains = function(item){
return RegExp("\\b"+item+"\\b").test(this);
};
例:
var category1Codes = new Array();
category1Codes.push(XX);
category1Codes.removeQua(XX);
if(category1Codes.contains(XX)){};
var params = {
'productCodes':category1Codes.join(',')
};