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
请尝试我的更新答案并从窗口中删除引号。这是否在您的浏览器中发出警报?我看不到警报。我也看不到。请查看新的窗口并调整其大小以检查宽度
。尝试我的更新答案并从窗口中删除引号。是吗他在你的浏览器中发出警报?我看不到警报。我也看不到。查看新的并调整窗口的大小以检查宽度。尝试我的更新答案并从窗口中删除引号。这是在你的浏览器中发出的警报吗?我看不到警报。我也看不到。查看新的并调整窗口的大小要检查宽度
。请尝试我的更新答案,并从窗口中删除引号
。这是否在您的浏览器中发出警报?我看不到警报。我也看不到。请查看新的并调整窗口的大小
以检查宽度
。