禁用函数jQuery

时间:2016-10-18 00:11:17

标签: javascript php jquery html function

在调整大小时,我要么是桌面电话,要么是移动电话。我在每个中都有一个点击功能,它们似乎都在运行,如果它在桌面上,然后更改为移动设备。如果移动功能为真,我需要关闭桌面功能,反之亦然。

这是我的代码的基本版本。

Screen_size()

$(window).resize(function() {
    Screen_size()
});

function Screen_size(){
    var w = $(window).width();
    if (w <= 710){
        Mobile();
    }

    if (w > 710){
        Desktop();
    }
}

function Mobile(){
    console.log('Mobile');
    $( "#open" ).click(function(e) {
        console.log('Open Mobile');
    });
}

function Desktop(){
    console.log('Desktop');
    $( "#open" ).click(function(e) {
        console.log('Open Desktop');
    });
}

1 个答案:

答案 0 :(得分:3)

不需要调整窗口大小..只需像这样点击事件

&#13;
&#13;
$( "#open" ).on('click', Screen_size);
    
function Screen_size(){
  var w = $(window).width();
  (w <= 710) ? Mobile() : Desktop() ;
}

function Mobile(){
  console.log('Open Mobile');
}

function Desktop(){
  console.log('Open Desktop');
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="open">Click</button>
&#13;
&#13;
&#13;