-1

我有这样的代码:

var IFS =
    document.isFullScreen || 
    document.webkitIsFullScreen || 
    document.mozIsFullScreen || 
    document.msIsFullScreen;

确切的问题是||门排序falseundefined,因为isFullScreen它是一个布尔值。

我该如何解决?

4

2 回答 2

1

如果每个元素都未定义,则 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;
于 2014-03-17T16:56:12.653 回答
-1

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];
于 2014-03-17T16:52:14.403 回答