-2

I can't find the error. Are you getting one?

You see an Object defined as an Object with 3 methods: gt (get), add (add), rm (remove). And afterwards I added an eventhandler (Ev) with just doing one thing: loading it on pageload and adding a click-event to an Element (elm).

var Obj = {
  gt: function(elm) {
    if (typeof === "string") {
    return document.getElementById(elm);  
    } else {
      return elm;
    }
  },
  add: function(elm, dst) {
    var elm = this.gt(elm);
    var dst = this.gt(dst);
    dst.appendChild(elm);
  },
  rm: function(elm) {
    var elm = this.gt(elm);
    elm.parentNode.removeChild(elm);
  }
};

var Ev = {
  add: function () {
    if (window.addEventListener) {
      return function(elm, type, fn) {
        Obj.gt(elm).addEventListener(type, fn, false);
      };
      // IE  <8 Suppport
    } else if (window.attachEvent) {
      return function(elm, type, fn){
        var f = function() {
          fn.call(Obj.gt(elm), window.event);
        };
        Obj.gt(elm).attachEvent('on' + type, f);
      };
    }
  }()
};

Ev.add(window, "load", function(){
 Ev.add("src", "click", function(){ // Source-Div with id "src"
  var elm = document.createElement("p");
  elm.innerHTML = "Test"; 
  Obj.add(elm, "dst"); // Destination-Div with id "dst"
  );
 });
});
4

1 回答 1

4

你漏了一个字:

if (typeof === "string") {

应该

if (typeof elm === "string") {
于 2013-08-06T09:19:04.383 回答