页面操作的简化

$ : function(id, scope) {
  if (arguments.length == 1 || !scope) {
   return document.getElementById(id);
  }
  var childNodes = scope.childNodes;
  var len = childNodes.length;
  for (var i = 0; i < len; i++) {
   if (childNodes[i].nodeType == 1) {
    if (childNodes[i].id == id) {
     return childNodes[i];
    }
    else {
     var returnNode = arguments.callee.apply(null, [id, childNodes[i]]);
     if (returnNode) {
      return returnNode;
     }
    }
   }
  }
  return null;
 },
 $attr:function(dom,attrName){
  try{
   return dom[attrName]!=null?dom[attrName]:dom.getAttribute(attrName);
  }catch(e){
    return null;
  }
 },
 $c : function(tagName) {
  return document.createElement(tagName);
 },
 $a : function(parentNode, childNode) {
  parentNode.appendChild(childNode);
 },
 $rs : function(selectElem, data) {
  if(!(selectElem&&data))
   return;
  var options = selectElem.options;
  var len = options.length;
  while (len--) {
   options.remove(len);
  }
  if (data && data.length != 0) {
   var l = data.length;
   for (var index = 0; index < l; index++) {
    var op = document.createElement("option");
    op.value = data[index].ids || data[index].id;
    op.text = data[index].name;
    options.add(op);
   }
   var firstValue = selectElem.options[0].value;
   if (firstValue && firstValue != "0")
    selectElem.value = selectElem.options[0].value;
   else if (data.length > 1) selectElem.value = selectElem.options[1].value;
  }
 },
 isDefined : function(obj) {
  return typeof obj != 'undefined' && !BusCard.util.isNull(obj);

 },
 isObject : function(obj) {
  return typeof obj == 'object' && !BusCard.util.isNull(obj);
 },
 isArray : function(obj) {

  return obj && obj.constructor == Array;
 },
 isNull : function(obj) {
  return obj === null;
 },
 isFunction : function(obj) {
  return typeof obj == 'function';
 },

 isString : function(obj) {
  return typeof obj == 'string';
 },
 getXhr : function() {
  var xhr = null;
  try {
   window.XMLHttpRequest ? xhr = new XMLHttpRequest() : xhr = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (e) {}
  return xhr;

 },

 parse : function(jsonData) {
  return (new Function("return " + jsonData)).apply(null, []);
 },

 

 

//函数中声明的所有变量在整个函数中都有定义。

例:

var scope = "global";

function f(){

    alert(scope);     //display "undefined"

    var scope = "local";

    alert(scope);   //display "local"

}

f();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值