js代码分离方法封装

var oldtd1;
var oldtd2;
var oldtd3;
var indexnum = 0;
//document.getebyid简写方法
function domByid(domid)
{
   return document.getElementById(domid);
}
function domsByclass(domclass)
{
    return document.getElementsByClassName(domclass);
}
//添加菜单方法
function Addmenu(foodlist)
{
  var inputtxt=  prompt("输入菜名", "例如:青椒肉丝");
  if (inputtxt.trim() == "" || inputtxt == null)
      return;
  var fdlist = domByid(foodlist).children;
  for (var i = 0; i < fdlist.length; i++) {
      if (inputtxt.trim() == fdlist[i].innerHTML.trim())
          return alert("重复菜名");
  }
 var newoption= document.createElement("option");
 newoption.innerHTML = inputtxt;
 domByid(foodlist).appendChild(newoption);
}
//添加订单方法
function Addorder(foodtable)
{
  
 var  trcreate=  document.createElement("tr");
 var trobject = new Object();
 var foodname= domByid("foodlist").value;
 var foodnum=parseInt( domByid("divin").children[1].innerHTML);
 indexnum++;
 trobject.td1 = "" + indexnum + "<input type=‘checkbox‘ class=‘ck01‘>";
 trobject.td2 = foodname;
 trobject.td3 = foodnum;
 trobject.td4 = "<a href=‘#‘ onclick=‘Deleteone(this);‘ >删除</a> <a href=‘#‘ onclick=‘Edit(this);‘ >编辑</a>";
 for (var i in trobject) {
     var tdcreate = document.createElement("td");
     tdcreate.innerHTML = trobject[i];
     trcreate.appendChild(tdcreate);
 }
 domByid(foodtable).appendChild(trcreate);
}
//单删除
function Deleteone(aobj)
{
    var trobj = aobj.parentNode.parentNode;
    trobj.parentNode.removeChild(trobj);
}
//多删除
function Deleteall(aobj)
{
    var trobjs = domsByclass("ck01");
    for (var i = trobjs.length-1; i >=0; i--) {
        if (trobjs[i].checked == true)
        {
           var trobj= trobjs[i].parentNode.parentNode;
           trobj.parentNode.removeChild(trobj);
        }
    }
}
//点击全选反选
function Checkall(ck)
{
    var cks = domsByclass("ck01");
    for (var i = 0; i <cks.length; i++) {
        cks[i].checked = ck.checked;
    }
}
//数量增加
function Addnum(foodnum)
{
    var num = parseInt(foodnum.parentNode.children[1].innerHTML.trim());
    num += 1;
    foodnum.parentNode.children[1].innerHTML = num;
}
function Cutnum(foodnum)
{
    var num = parseInt(foodnum.parentNode.children[1].innerHTML.trim());
    num -= 1;
    foodnum.parentNode.children[1].innerHTML = num;
}
//编辑
function Edit(aobj)
{
    var trobj = aobj.parentNode.parentNode;
    var foodlist= domByid("foodlist");
    var divin = domByid("divin");
    oldtd1 = trobj.children[1].innerHTML;
    trobj.children[1].innerHTML = "";
    trobj.children[1].appendChild(foodlist.cloneNode(true));
    trobj.children[1].children[0].value = oldtd1;
    oldtd2 = trobj.children[2].innerHTML;
    trobj.children[2].innerHTML = "";
    trobj.children[2].appendChild(divin.cloneNode(true));
    trobj.children[2].children[0].children[1].innerHTML = oldtd2;
    oldtd3 = trobj.children[3].innerHTML;
    trobj.children[3].innerHTML= "<a href=‘#‘ onclick=‘Save(this);‘>保全</a> <a href=‘#‘ onclick=‘Cancel(this);‘>取消</a>";
}
//保全
function Save(aobj)
{
    var trobj = aobj.parentNode.parentNode;
    
    var foodname = trobj.children[1].children[0].value
    trobj.children[1].innerHTML = foodname;
    var foodnum = trobj.children[2].children[0].children[1].innerHTML;
    trobj.children[2].innerHTML = foodnum;
    trobj.children[3].innerHTML = "<a href=‘#‘ onclick=‘Deleteone(this);‘ >删除</a> <a href=‘#‘ onclick=‘Edit(this);‘ >编辑</a>";
}

//取消
function Cancel(aobj)
{
    var trobj = aobj.parentNode.parentNode;
    trobj.children[1].innerHTML = oldtd1;
    trobj.children[2].innerHTML = oldtd2;
    trobj.children[3].innerHTML = oldtd3;
}
js代码方法封装

 

0
序号 菜名 数量 操作

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。