Javascript 使用我自己的功能检测媒体设备

Javascript 使用我自己的功能检测媒体设备,javascript,jquery,Javascript,Jquery,为什么下面的代码不起作用 var detectDevice; var mlg = function(){ $(window).on('load resize',function(){ if($(this).width >= 768){ return detectDevice = 'mlg'; } }) } if(detectDevice=='mlg'){

为什么下面的代码不起作用

 var detectDevice;
    var mlg = function(){
        $(window).on('load resize',function(){
            if($(this).width >= 768){
                return detectDevice = 'mlg';
            }
        })
    }
    if(detectDevice=='mlg'){alert('test')}

更新:我需要使用如下内容:

var detectDevice;
var mlg = function(){
 //  detectDevice = 'mlg';
}
var mxs = function(){
 //  detectDevice = 'mxs';
}
if(detectDevice=='mlg'){
alert('test');
}
是一个函数试试这个

$(window).on('load resize',function(){// remove quotes from window
   if($(this).width() >= 768){ //use width() and 768 rempve px
       detectDevice = 'mlg';// remove return
   }
});

是一个函数,请尝试以下操作

$(window).on('load resize',function(){// remove quotes from window
   if($(this).width() >= 768){ //use width() and 768 rempve px
       detectDevice = 'mlg';// remove return
   }
});

是一个函数,请尝试以下操作

$(window).on('load resize',function(){// remove quotes from window
   if($(this).width() >= 768){ //use width() and 768 rempve px
       detectDevice = 'mlg';// remove return
   }
});

是一个函数,请尝试以下操作

$(window).on('load resize',function(){// remove quotes from window
   if($(this).width() >= 768){ //use width() and 768 rempve px
       detectDevice = 'mlg';// remove return
   }
});

width
是方法而不是属性。因此它应该是
width()
。也可以使用
$(窗口)
而不是$('window'):


width
是方法而不是属性。因此它应该是
width()
。也可以使用
$(window)
而不是$('window'):


width
是方法而不是属性。因此它应该是
width()
。也可以使用
$(window)
而不是$('window'):


width
是方法而不是属性。因此它应该是
width()
。也可以使用
$(window)
而不是$('window'):

试试这个

var detectDevice;
function mlg(){
    if($(window).width() >= 768){
        detectDevice = 'mlg';
        if(detectDevice=='mlg'){
            alert('test');
        }
    };
};
$(window).on('load resize', function(){mlg();});
试试这个

var detectDevice;
function mlg(){
    if($(window).width() >= 768){
        detectDevice = 'mlg';
        if(detectDevice=='mlg'){
            alert('test');
        }
    };
};
$(window).on('load resize', function(){mlg();});
试试这个

var detectDevice;
function mlg(){
    if($(window).width() >= 768){
        detectDevice = 'mlg';
        if(detectDevice=='mlg'){
            alert('test');
        }
    };
};
$(window).on('load resize', function(){mlg();});
试试这个

var detectDevice;
function mlg(){
    if($(window).width() >= 768){
        detectDevice = 'mlg';
        if(detectDevice=='mlg'){
            alert('test');
        }
    };
};
$(window).on('load resize', function(){mlg();});


因此
if($(this).width>=768px){
应该是
if($(this).width()>=768){
$(this)是针对窗口的,不是吗?基本上,你的代码有错误的语法和错误的函数调用,所以预期的结果是在加载/调整大小后,如果宽度>=768,那么警告“Test”,对吗?因为
if($(this).width>=768px){
应该是
if($(this).width()>=768){
$(this)是针对窗口的,不是吗?基本上,你的代码有错误的语法和错误的函数调用,所以预期的结果是在加载/调整大小之后,如果宽度>=768,那么警告“Test”,对吗?因为
if($(this).width>=768px){
应该是
if($(this).width()>=768){
$(这)是针对窗口的,不是吗?基本上,你的代码有错误的语法和错误的函数调用,所以预期的结果是在加载/调整大小后,如果宽度>=768,那么警告“测试”,对吗?因为这
if($(this).width>=768px){
应该是
if($(this).width()>=768){
$(this)是针对窗口的,不是吗?基本上,您的代码有错误的语法和错误的函数调用,因此预期的结果是在加载/调整大小之后,如果宽度>=768,则警告“测试”是吗?@BangDao:没有看到。谢谢。你也忘了用
768
替换
768px
768
没有看到。谢谢。你也忘了用
768
替换
768px
没有看到。谢谢。你也忘了用
768
替换
768px
没有看到。谢谢。你也没有看到orgot将
768px
替换为
768
请尝试我的更新答案并从窗口中删除
引号。这是否在您的浏览器中发出警报?我看不到警报。我也看不到。请查看新的
窗口并调整其大小以检查
宽度
。尝试我的更新答案并从窗口中删除
引号。是吗他在你的浏览器中发出警报?我看不到警报。我也看不到。查看新的并调整
窗口的大小以检查
宽度
。尝试我的更新答案并从窗口中删除
引号
。这是在你的浏览器中发出的警报吗?我看不到警报。我也看不到。查看新的并调整
窗口的大小
要检查
宽度
。请尝试我的更新答案,并从窗口中删除
引号
。这是否在您的浏览器中发出警报?我看不到警报。我也看不到。请查看新的并调整
窗口的大小
以检查
宽度