Actually,simplicity is not simple

contains()函数使用一例

今天看到的,让我了解了下compareDocumentPosition()contains()函数,以后判断是否在元素上面就有新方法了。其它的也没什么好说,看下Demo吧!

window.onload = function () {
    document.getElementById('dropdown').onmouseover = openMenu;
}

function openMenu(e) {
    var evt = e || window.event;
    var tgt = evt.target || evt.srcElement;
    while (tgt.nodeName !== 'LI') {
        if (tgt.nodeName === 'HTML') {
            return;
        }
        tgt = tgt.parentNode;
    }
    var nested = tgt.getElementsByTagName('ul')[0];
    if (nested) {
        nested.className = 'open';
        tgt.onmouseout = function (e) {
            var evt = e || window.event;
            var related = evt.relatedTarget || evt.toElement;
            if (!nested.contains(related)) {
                nested.className = '';
                tgt.onmouseout = null;
            }
        }
    }
}

/* Bloody Firefox STILL doesn't support contains */

if (window.Node && Node.prototype && !Node.prototype.contains) {
    Node.prototype.contains = function (arg) {
        return !!(this.compareDocumentPosition(arg) & 16)
    }
}

日志信息 »

该日志于2010-05-23 18:36由 asins 发表在Develop分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过评论 RSS订阅这个日志的所有评论。

相关日志 »

发表评论 »

captcha
请输入验证码