我有这样的代码:
var IFS =
document.isFullScreen ||
document.webkitIsFullScreen ||
document.mozIsFullScreen ||
document.msIsFullScreen;
确切的问题是||
门排序false
为undefined
,因为isFullScreen
它是一个布尔值。
我该如何解决?
我有这样的代码:
var IFS =
document.isFullScreen ||
document.webkitIsFullScreen ||
document.mozIsFullScreen ||
document.msIsFullScreen;
确切的问题是||
门排序false
为undefined
,因为isFullScreen
它是一个布尔值。
我该如何解决?
如果每个元素都未定义,则 IFS 将未定义。由于 undefined 是一个假值,您仍然可以将条件语句设为:
var IFS =
document.isFullScreen ||
document.webkitIsFullScreen ||
document.mozIsFullScreen ||
document.msIsFullScreen;
if(!IFS){
console.log('not full screen');
}
如果您仍想保留该false
值以防其他所有变量为undefined
,那么您可以使用以下内容:
var IFS =
document.isFullScreen ||
document.webkitIsFullScreen ||
document.mozIsFullScreen ||
document.msIsFullScreen ||
false;
undefined
是一个虚假值,这意味着||
操作员将转换将认为它与false
.
如果您只想找到未定义的第一个值,您可以尝试以下操作:
var IFS =
(typeof document.isFullScreen != "undefined") ? document.isFullScreen :
(typeof document.webkitIsFullScreen != "undefined") ? document.webkitIsFullScreen :
(typeof document.mozIsFullScreen != "undefined") ? document.mozIsFullScreen :
(typeof document.msIsFullScreen != "undefined") ? document.msIsFullScreen :
false;
或者正如Niet所建议的那样:
var IFS =
('isFullScreen' in document) ? document.isFullScreen :
('webkitIsFullScreen' in document) ? document.webkitIsFullScreen :
('mozIsFullScreen' in document) ? document.mozIsFullScreen :
('msIsFullScreen' in document) ? document.msIsFullScreen :
false;
或者你可以使用一个数组,像这样:
var arr = ['isFullScreen', 'webkitIsFullScreen', 'mozIsFullScreen', 'msIsFullScreen'];
var IFS = false;
for (var i = 0; i < arr.length; i++) {
if (arr[i] in document) {
IFS = document[arr[i]];
break;
}
}
或者像这样
var arr = ['isFullScreen', 'webkitIsFullScreen', 'mozIsFullScreen', 'msIsFullScreen'];
var key = arr.filter(function(e) { return e in document; })[0];
var IFS = !!document[key];