素人がプログラミングを勉強していたブログ

プログラミング、セキュリティ、英語、Webなどのブログ since 2008

連絡先: twitter: @javascripter にどうぞ。

リンクのURL、画像のalt属性をポップアップ(jQuery風味)

jkQueryっていう名前に特に意味はない。かなり大げさだけど、書いてみたよってことで。

function jkQuery(selector, context) {
  if (!(this instanceof jkQuery)) return new jkQuery(selector, context);
  return jkQuery.prototype.init.apply(this, arguments);
}
jkQuery.extend = function(base, source) {
  for (var i in source)
    if (source.hasOwnProperty(i)) {
      var getter = source.__lookupGetter__(i),
      setter = source.__lookupSetter__(i);
      if (getter)
        base.__defineGetter__(i, getter);
        else if (setter)
          base.__defineSetter__(i, setter);
          else
            base[i] = source[i];
   }
  return base;
}
jkQuery.extend(jkQuery, {
  each: function(obj, fn) {
    if ("0" in obj && "length" in obj) {
      Array.forEach(obj,fn);
    } else
      for(var i in obj)
        if (obj.hasOwnProperty(i))
          fn(i, obj[i]);
  },
  push: function(obj, arr) {
    Array.prototype.push.apply(obj, arr);
  },
  toArray: function(arr) {
    return Array.slice(arr);
  }
});
jkQuery.extend(jkQuery.prototype, {
  init: function(selector, context) {
    if (selector.nodeType) {
      this[0] = selector;
      this.length = 1;
      return this;
    }
    jkQuery.push(this,
      jkQuery.toArray((context || document).querySelectorAll(selector)));
    return this;
  },
  toArray: function()
    jkQuery.toArray(this),
  css: function(prop, val) {
    if (typeof prop != "object") {
      var _prop = prop;
      prop = {};
      prop[_prop] = val;
    }
    jkQuery.each(this,
      function(elem) {
        jkQuery.each(prop,
          function(key, val) {
            elem.style[key] = val;
          });
      });
    return this;
  },
  bind: function(type, handler, useCapture) {
    jkQuery.each(this,
      function(elem) {
        elem.addEventListener(type, handler, useCapture);
      });
    return this;
  },
  unbind: function(type, handler, useCapture) {
    jkQuery.each(this,
      function(elem) {
        elem.removeEventListener(type, handler, useCapture);
      });
    return this;
  },
  html: function(text) {
    jkQuery.each(this,
      function(elem) {
        elem.innerHTML = text;
      });
    return this;
  },
  text: function(text) {
    jkQuery.each(this,
      function(elem) {
        elem.textContent = text;
      });
    return this;
  },
  attr: function(attr,val) {
    jkQuery.each(this,
      function(elem) {
        elem.setAttribute(attr, val);
      });
    return this;
  },
  each: function(fn) {
    jkQuery.each(this, fn);
    return this;
  }
});


jkQuery("a[href]:not([title]), img[alt]:not([title])").
  each(function(elem) {
    jkQuery(elem).attr("title", elem.alt || elem.href);
  });