元素或模式上的链接在点击时不起作用

问题描述 投票:0回答:1

我使用 Elementor 的弹出功能和动态功能创建了一个汉堡菜单,将弹出窗口与汉堡包连接起来,以便每当我单击汉堡包按钮时它都会触发菜单。但是,由于某种原因,我无法处理其中链接的点击。我想有些东西阻止了它,因为我没有任何错误。我的 jQuery 运行良好,如果我定位页面上的另一个元素(例如“entry-title”),则单击会起作用。因此,如果您知道该怎么做,我请求您的帮助。这是我的代码:

Navigation.js

    jQuery(document).ready(function($) {
console.log('Here it works');
// Navigation menu
jQuery('.onepage h4.elementor-heading-title.elementor-size-default > a').click(function(event) {
    console.log("here doesn't work");        
    event.preventDefault();
    var hash = this.hash;

    if (window.location.pathname === '/') {
        jQuery('html, body').animate({
            scrollTop: jQuery(hash).offset().top
        }, 800, function() {
            window.location.hash = hash;
        });
    } else {
        var targetUrl = window.location.origin + '/#' + this.hash.substr(1);
        window.location.href = targetUrl;
        return;
    }
    jQuery('.elementor-popup-modal').css('display', 'none');
});

});

jquery menu popup elementor
1个回答
0
投票

我终于用这段代码解决了问题

   <script>
   jQuery(function($) {
    // Attache un gestionnaire d'événements de clic à tous les liens à l'intérieur de ".elementor-location-popup"
    $(document).on('click', '.elementor-location-popup a', function(event) {
        // Ferme la popup
        elementorProFrontend.modules.popup.closePopup({}, event);
        
        // Vérifie si le lien a la classe ".onepage"
        if ($(this).is('.onepage')) {
            console.log('test');
            // Empêche le comportement par défaut du lien
            event.preventDefault();
            
            // Obtient le hash du lien
            var hash = this.hash;

            // Vérifie le chemin de l'URL actuelle
            if (window.location.pathname === '/') {
                // Anime le défilement vers la position du hash
                $('html, body').animate({
                    scrollTop: $(hash).offset().top
                }, 800, function() {
                    // Met à jour l'URL avec le hash
                    window.location.hash = hash;
                });
            } else {
                // Construit l'URL cible avec le hash
                var targetUrl = window.location.origin + '/#' + hash.substr(1);
                // Redirige vers l'URL cible
                window.location.href = targetUrl;
                return;
            }
        }
    });
});


</script>

我还通过插入自定义 HTML 标签来直接创建标签来解决这个问题,这使我能够将 .onepage 类直接添加到我的标签中并赋予它们所需的行为。

© www.soinside.com 2019 - 2024. All rights reserved.