首页 > 这个闭包我想传多个参数进去该怎么改进?

这个闭包我想传多个参数进去该怎么改进?

    var cssSupports = (function() {
    var div = document.getElementById('cultural-banner'),
        vendors = 'Khtml O Moz Webkit'.split(' '),
        len = vendors.length;
    return function(prop) {
        if ( prop in div.style ) return true;
        if ('-ms-' + prop in div.style) return true;
        
        prop = prop.replace(/^[a-z]/, function(val) {
            return val.toUpperCase();
        });

        while(len--) {
            if ( vendors[len] + prop in div.style ) {
            return true;
        }
    }
        return false;
    };
})();
console.log(cssSupports('transition'));

想要实现的效果就是cssSupports(elem,property)在里面传一个选择器和一个属性名,该怎么改?


直接这样不好么?

var cssSupports = function(selector, property) {
    var selected = document.querySelector(selector),
        vendors = 'Khtml O Moz Webkit'.split(' '),
        len = vendors.length;
    if (property in selected.style) {
        return true;
    }
    if ('-ms-' + property in selected.style) {
        return true;
    }

    property = property.replace(/^[a-z]/, function(val) {
        return val.toUpperCase();
    });

    while (len--) {
        if (vendors[len] + property in selected.style) {
            return true;
        }
    }
    return false;
};
console.log(cssSupports('#cultural-banner', 'transition'));
【热门文章】
【热门文章】