简体   繁体   English

$(document).on('click') 在某些移动设备上不起作用?

[英]$(document).on('click') not working on some mobile devices?

I've got a script that's using .on() because an element is dynamically generated, and it isn't working on some mobile devices.我有一个使用 .on() 的脚本,因为一个元素是动态生成的,它在某些移动设备上不起作用。 You can try lilwhats.com/demo please use this website to test it out: https://www.browserling.com/ choose an older version of android or you can choose Windows Vista > Internet Explorer v9您可以尝试 lilwhats.com/demo 请使用此网站进行测试: https ://www.browserling.com/ 选择旧版本的 android 或您可以选择 Windows Vista > Internet Explorer v9

restaurant.php餐厅.php

<?php
($allow_order
    ? '<div class="add-menu">
            <div class="add-btn add-item-to-order">
                <span>' . $lang['ADD'] . '</span>
                <i class="icon-feather-plus"></i>
            </div>
' .
    (!empty($extras) ? '<span class="customize">' . $lang['CUSTOMIZABLE'] . '</span>' : '')
    . '        </div>'
    : '') .

'

theme.js主题.js

// add menu to order
$(document).on('click','.add-item-to-order', function (e) {
    e.preventDefault();

    var $add_customize = $('<div class="add-btn add-item-btn">' +
        '<div class="wrapper h-100">' +
        '<div class="addition menu-order-quantity-decrease">' +
        '<i class="icon-feather-minus"></i>' +
        '</div>' +
        '<div class="count">' +
        '<span class="num menu-order-quantity">1</span>' +
        '</div>' +
        '<div class="addition menu-order-quantity-increase">' +
        '<i class="icon-feather-plus"></i>' +
        '</div>' +
        '</div>' +
        '</div>'),
        $item = $(this).closest('.section-menu'),
        item_id = $item.data('id'),
        name = $item.data('name'),
        description = $item.data('description'),
        price = $item.data('price'),
        amount = $item.data('amount'),
        order_price = Number(amount),
        extras = TOTAL_MENUS[$item.data('id')].extras || [],
        order_data = JSON.parse(localStorage.getItem('quickqr_order')),
        random_id = randomId(10);


    $item.data('cart_id',random_id);
    if(extras.length == 0){
        order_data[random_id] = {
            'id': item_id,
            'item_name': name,
            'item_price': amount,
            'extras': {},
            'quantity': 1
        };

        localStorage.setItem('quickqr_order', JSON.stringify(order_data));

        $item.find('.add-menu').html($add_customize);
    } else {
        $menuCustomize.find('h4').html(name);
        $menuCustomize.find('.customize-item-description').html(description);
        $('#order-price').html(formatPrice(amount));
        $('#menu-order-quantity').text(1);

        var $extra_wrapper = $('#customize-extras');
        $extra_wrapper.html('');

        for (var i in extras) {
            if (extras.hasOwnProperty(i)) {
                var $extra_tpl = $(
                    '<div class="extras menu-extra-item">' +
                    '<span class="extra-item-title"></span>' +
                    '<div class="d-flex align-items-center">' +
                    '<span class="mr-2 extra-item-price"></span>' +
                    '<div class="custom-control custom-checkbox mr-sm-2">' +
                    '<input type="checkbox" class="custom-control-input" id="customControl">' +
                    '<label class="custom-control-label" for="customControl"></label>' +
                    '</div>' +
                    '</div>' +
                    '</div>');

                $extra_tpl.find('.custom-control-input').attr('id', 'checkbox' + extras[i].id);
                $extra_tpl.find('label').attr('for', 'checkbox' + extras[i].id);
                $extra_tpl.find('.extra-item-title').html(extras[i].title);
                $extra_tpl.find('.extra-item-price').html(formatPrice(extras[i].price));
                $extra_tpl.data('price', extras[i].price);
                $extra_tpl.data('id', extras[i].id);

                $extra_tpl.find('.custom-control-input').on('change',function () {
                    $('#menu-order-quantity').text(1);
                    calculateOrderPrice(amount);

                });
                $extra_wrapper.append($extra_tpl);
            }
        }

        $menuCustomize.find('.menu-order-quantity-decrease').off().on('click', function (e) {
            e.stopPropagation();
            var quatity = Number($('#menu-order-quantity').text()) - 1;
            if(quatity == 0){
                quatity = 1;
            }
            $('#menu-order-quantity').text(quatity);
            calculateOrderPrice(order_price);
        });
        $menuCustomize.find('.menu-order-quantity-increase').off().on('click', function (e) {
            e.stopPropagation();
            $('#menu-order-quantity').text(Number($('#menu-order-quantity').text()) + 1);
            calculateOrderPrice(order_price);
        });

        $('#add-order-button').off().on('click', function (e) {
            calculateOrderPrice(order_price);
            var price = $('#order-price').html();
            var order_data = JSON.parse(localStorage.getItem('quickqr_order'));

            // this order's extras
            var extras = {};
            $('.menu-extra-item').each(function () {
                if($(this).find('.custom-control-input').is(':checked')){
                    extras[randomId(10)] = {
                        'id': $(this).data('id'),
                        'name': $(this).find('.extra-item-title').html(),
                        'price': $(this).data('price')
                    };
                }
            });

            order_data[random_id] = {
                'id': item_id,
                'item_name': name,
                'item_price': amount,
                'extras': extras,
                'quantity': $('#menu-order-quantity').text()
            };

            localStorage.setItem('quickqr_order', JSON.stringify(order_data));

            $add_customize.find('.menu-order-quantity').text($('#menu-order-quantity').text());
            $item.find('.add-menu').html($add_customize);

            manageViewOrder();
            $('.overlay').trigger('click');
        });


        $menuCustomize.addClass('active');
        $('.overlay').addClass('active');
        $('body').addClass('noscroll');
    }
    manageViewOrder();
});

I really appreciate any help我真的很感激任何帮助

$order_tpl.find('.menu-order-quantity-decrease').off().on('click', function (e) {
    e.stopPropagation();
    var $item = $(this).closest('.section-menu');
    var $quantity = $item.find('.menu-order-quantity');
    var quantity = Number($quantity.text()) - 1;
    var cart_key = $item.data('cart_id');
    var $menu_item = $('.section-menu[data-id="' + order_data[cart_key]['id'] + '"]');
    if (quantity == 0) {
        var $add_btn = $('<div class="add-btn add-item-to-order">' +
            '<span>' + LANG_ADD + '</span>' +
            '<i class="icon-feather-plus"></i>' +
            '</div>');
        $menu_item.find('.add-menu').html($add_btn);
        delete order_data[cart_key];

    } else {
        $menu_item.find('.menu-order-quantity').text(quantity);
        order_data[cart_key]['quantity'] = quantity;
    }

    localStorage.setItem('quickqr_order', JSON.stringify(order_data));
    generateViewOrder();
});

There is no mouse on a phone or tablet so try to add events to you handler手机或平板电脑上没有鼠标,因此请尝试向您的处理程序添加事件

$(document).on("click tap touchstart", "element", function(e){
 //code here 
}

jQuery Current Active Browser Support jQuery 当前活动浏览器支持

Desktop桌面
Chrome: (Current - 1) and Current Chrome:(当前 - 1)和当前
Edge: (Current - 1) and Current边缘:(当前 - 1)和当前
Firefox: (Current - 1) and Current, ESR Firefox:(当前 - 1)和当前,ESR
Internet Explorer: 9+浏览器:9+
Safari: (Current - 1) and Current Safari:(当前 - 1)和当前
Opera: Current歌剧:当前

Mobile移动的
Stock browser on Android 4.0+[1] Android 4.0+[1] 上的股票浏览器
Safari on iOS 7+ iOS 7+ 上的 Safari

If you need to support older browsers like Internet Explorer 6-8, Opera 12.1x or Safari 5.1+, use jQuery 1.12 .如果您需要支持较旧的浏览器,例如 Internet Explorer 6-8、Opera 12.1x 或 Safari 5.1+,请使用jQuery 1.12

For more details please checkout this link .有关更多详细信息,请查看此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM