跨浏览器函数

function getEventTarget(event)
{
var targetElement = null;

if (typeof event.target != "undefined")
{
    targetElement = event.target;
}
else
{
    targetElement = event.srcElement;
}

while (targetElement.nodeType == 3 && targetElement.parentNode != null)
{
    targetElement = targetElement.parentNode;
}
return targetElement;

}

事件目标函数getEventTarget:综合了IE和W3C标准方法,找出当前事件的目标,由于事件从捕获阶段开始查找,很可能会找到子接点.所以用循环来查找上一级.

 

function attachEventListener(target, eventType, functionRef, capture)
{
if (typeof target.addEventListener != "undefined")
{
    target.addEventListener(eventType, functionRef, capture);
}
else if (typeof target.attachEvent != "undefined")
{
    target.attachEvent("on" + eventType, functionRef);
}
else
{
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldListener = target[eventType];

      target[eventType] = function()
      {
        oldListener();

        return functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
}

return true;
}

事件目标函数attachEventListener:综合了IE和W3C标准方法,为某元素添加一个事件.

 

function addLoadListener(fn)
{
if (typeof window.addEventListener != 'undefined')
{
    window.addEventListener('load', fn, false);
}
else if (typeof document.addEventListener != 'undefined')
{
    document.addEventListener('load', fn, false);
}
else if (typeof window.attachEvent != 'undefined')
{
    window.attachEvent('onload', fn);
}
else
{
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
}
}

addLoadListener:综合了IE和W3C标准方法,为文档添加一个load事件.

 

function getElementsByAttribute(attribute, attributeValue)
{
var elementArray = new Array();
var matchedArray = new Array();

if (document.all)
{
    elementArray = document.all;
}
else
{
    elementArray = document.getElementsByTagName("*");
}

for (var i = 0; i < elementArray.length; i++)
{
    if (attribute == "class")
    {
      var pattern = new RegExp("(^| )" + attributeValue + "( |$)");

      if (elementArray[i].className.match(pattern))
      {
        matchedArray[matchedArray.length] = elementArray[i];
      }
    }
    else if (attribute == "for")
    {
      if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for"))
      {
        if (elementArray[i].htmlFor == attributeValue)
        {
          matchedArray[matchedArray.length] = elementArray[i];
        }
      }
    }
    else if (elementArray[i].getAttribute(attribute) == attributeValue)
    {
      matchedArray[matchedArray.length] = elementArray[i];
    }
}

return matchedArray;
}

getElementsByAttribute的功能为通过(for或则class)来查找元素.

 

function getScrollingPosition()
{
//array for X and Y scroll position
var position = [0, 0];

//if the window.pageYOffset property is supported
if(typeof window.pageYOffset != 'undefined')
{
    //store position values
    position = [
        window.pageXOffset,
        window.pageYOffset
    ];
}

//if the documentElement.scrollTop property is supported
//and the value is greater than zero
if(typeof document.documentElement.scrollTop != 'undefined'
    && document.documentElement.scrollTop > 0)
{
    //store position values
    position = [
        document.documentElement.scrollLeft,
        document.documentElement.scrollTop
    ];
}

//if the body.scrollTop property is supported
else if(typeof document.body.scrollTop != 'undefined')
{
    //store position values
    position = [
        document.body.scrollLeft,
        document.body.scrollTop
    ];
}

//return the array
return position;
}

getScrollingPosition函数的功能:取得当前页面的滚动位置大小

 

function function getViewportSize()
{
var size = [0,0];

if (typeof window.innerWidth != 'undefined')
{
    size = [
        window.innerWidth,
        window.innerHeight
    ];
}
else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth != 'undefined'
      && document.documentElement.clientWidth != 0)
{
    size = [
        document.documentElement.clientWidth,
        document.documentElement.clientHeight
    ];
}
else
{
    size = [
        document.getElementsByTagName('body')[0].clientWidth,
        document.getElementsByTagName('body')[0].clientHeight
    ];
}

return size;
}()
{
var size = [0,0];

if (typeof window.innerWidth != 'undefined')
{
    size = [
        window.innerWidth,
        window.innerHeight
    ];
}
else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth != 'undefined'
      && document.documentElement.clientWidth != 0)
{
    size = [
        document.documentElement.clientWidth,
        document.documentElement.clientHeight
    ];
}
else
{
    size = [
        document.getElementsByTagName('body')[0].clientWidth,
        document.getElementsByTagName('body')[0].clientHeight
    ];
}

return size;
}

getViewportSize:窗口可视区域的大小

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值