为移动设备禁用 jQuery [英] Disable jQuery for mobile devices

查看:30
本文介绍了为移动设备禁用 jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为移动设备禁用部分 jQuery 脚本,以下是我想禁用的代码:

I would like to disable part of my jQuery script for mobile devices, heres the code I'd like to disable:

$('#inner-slide1').click(function(e) {
    e.preventDefault();
    dataslide = $(this).attr('data-slide');
    goToByScroll(dataslide);
});

$('#inner-slide2').click(function(e) {
    e.preventDefault();
    dataslide = $(this).attr('data-slide');
    goToByScroll(dataslide);
});

$('#inner-slide3').click(function(e) {
    e.preventDefault();
    dataslide = $(this).attr('data-slide');
    goToByScroll(dataslide);
});

$('#inner-slide4').click(function(e) {
    e.preventDefault();
    dataslide = $(this).attr('data-slide');
    goToByScroll(dataslide);
});

$('#inner-slide5').click(function(e) {
    e.preventDefault();
    dataslide = $(this).attr('data-slide');
    goToByScroll(dataslide);
});

感谢您的任何建议.

推荐答案

如果你只需要给这些元素一个类,你就需要重复执行 5 次函数(我要使用 .内滑动):

You're repeating a function 5 times when you need only do it once if you just give those elements a class (I'm going for .inner-slide):

var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ? true : false;

$('.inner-slide').click(function(e) {
    if(!isMobile) {
        e.preventDefault();
        dataslide = $(this).attr('data-slide');
        goToByScroll(dataslide);
    }
});

if 语句只会在移动设备(Android、webOS、iPhone、iPad、iPod、Blackberry)上导致为真

The if statement will only result to true on a mobile device (Android,webOS,iPhone,iPad,iPod,Blackberry)

请注意

$(this).attr('data-slide');

也可以写成:

$(this).data('slide');

这篇关于为移动设备禁用 jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆