码迷,mamicode.com
首页 > Web开发 > 详细

js代码分离方法封装

时间:2014-11-19 20:12:16      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   java   

bubuko.com,布布扣
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
序号 菜名 数量 操作

js代码分离方法封装

标签:style   blog   http   io   ar   color   os   sp   java   

原文地址:http://www.cnblogs.com/zhanying/p/4108856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!