最近在做公司项目中的(后台管理项目)前端,涉及到jq dom太多了 来这记录下
重点记录之前没用过的方法:
//找到某一特定的祖先元素parent("td")
//层级选择器
var inp=$("input[type='checkbox']"); //返回所有 input type=checkbox
var check=$("input[name='ckbox']:checked"); //选中的复选框
var index=obj.selectedIndex; //获取select选中位置的index
localStorage
//这个是谷歌的一个方法,可以直接调用类似cookie session域
setItem 参数为 Key,Value 键值对形式
getItem 参数 Key值
removeItem 删除Key值
1.全选/全不选
//表头的 单选按钮
var checkbox = document.getElementById("checkbox");
var ckbox=document.getElementsByName("ckbox");
checkbox.οnclick=function(){
for ( var index = 0; index < ckbox.length; index++) {
//全选
if (checkbox.checked) {
console.debug(checkbox.checked);
var ck=ckbox[index];
ck.checked= true;
} else {
//全不选
console.debug(checkbox.checked);
var ck=ckbox[index];
ck.checked= false;
}
}
}
2.批量删除(根据id删除)
//批量删除
var batch_del=document.getElementById("batch_del");
//实例化 xw
var xwArr=new Array();
batch_del.οnclick=function(){
var j=0;
// {"xh":"12","bt":"第N2次世界大战","fl":"普通通知","ly":"打野","gxsj":"2017-03-01","llcs":666}
var inp=$("input[type='checkbox']");//返回所有 input type=checkbox
var check=$("input[name='ckbox']:checked");//选中的复选框
check.each(function(){
//debugger;
j=j+1;
var row=$(this).parent("td").parent("tr");
xh=row.find("[name='xh']").html();//序号
xwArr[j]=xh;
debugger;
bt=row.find("[name='bt']").html(); //标题
fl=row.find("[name='fl']").html();//分类
ly=row.find("[name='ly']").html(); //来源
gxsj=row.find("[name='gxsj']").html();//更新时间
llcs=row.find("[name='llcs']").html(); //浏览次数
console.debug("xwArr[index]:"+xwArr[j]);
});
//var xw=JSON.stringify(xwArr);
if(j<=0){
alert("请选择要删除的数据!");
}else{
//删除请求
$.ajax({
url:zc+"",
type:"POST",
data:{
xw:xwArr
},
success:function(){
},
error:function(){
alert("网络错误");
}
});
}
};
单行删除
//删除行
function delrow(obj1){
var row1 = obj1.parentNode.parentNode; //按钮所在行
var id=$("[name='indexID']").attr("id"); //通过name 获取 id
var arr=row1.cells; //行内元素
var xh=arr[0]; //行内元素 第index 位置上的元素
var xhval=xh.innerHTML;
//debugger;
//删除请求
$.ajax({
url:zc+"scgrjlggxx",
type:"POST",
data:{
ryid:ryid,lx:lx,id:id
},
success:function(msg){
if(msg=="ok"){
alert("删除成功!")
}else{
alert("网络异常,请重新操作!")
}
window.location.reload();
},
error:function(){
alert("网络错误");
}
});
//alert("删除行")
}