我最近看了关于CSS的浏览器功能检测的教程......最终的产品是这样的......foo in bar - 'in'operator javascript?
var prefix = ['Moz', 'webkit', 'O', 'ms', 'Khtml'],
test_style = document.createElement('div').style;
var css_check = function(prop) {
if (prop in test_style) {
return true;
}
for (var i = 0; i < prefix.length; i++) {
if (prefix[i] + prop in test_style) {
return true;
}
}
return false;
};
css_check('whatev_css_property');
我不明白的部分是这个...
if (prop in test_style)
或if (foo in bar)
。
从我读的if (foo in bar)
是用来检查一个值是否在一个数组中,但我可能在这里是错误的,我还没有找到这方面的文档。此外,如果这是用来检查数组中的值如何是test_style = document.createElement('div').style
数组?没有意义...
我很可怕的困惑。任何澄清将不胜感激。
我不会建议使用'foo in bar'来检查数组中是否存在值 - 您应该使用'indexOf'(http://stackoverflow.com/questions/237104/array-containsobj-in- javascript)。在你的情况下,'test_style'是一个对象字面值,在这种情况下'foo in bar'对于检查密钥的存在是正确的。在bar中使用'foo'检查数组中是否存在索引(索引与对象的键相同) – Ian