简体   繁体   English

使用jQuery将选择标签附加到菜单会导致菜单在wordpress中加倍

[英]using jQuery toappend select tags to a menu causes the menu to double in wordpress

in a js file called via wp_enqueue_script i have this: 在通过wp_enqueue_script调用的js文件中,我有以下内容:

jQuery(function () {
    jQuery("<select />").appendTo("#primary_menu_mobile");
    jQuery("<option />", {
        "selected": "selected",
            "value": "",
            "text": "Go to..."
    }).appendTo("#primary_menu_mobile select");
    jQuery("#primary_menu_mobile a").each(function () {
        var el = jQuery(this);
        jQuery("<option />", {
            "value": el.attr("href"),
                "text": el.text()
        }).appendTo("#primary_menu_mobile select");
    });
    jQuery("#primary_menu_mobile select").change(function () {
        window.location = jQuery(this).find("option:selected").val();
    });
});

in my header.php i call upon the menu like this: 在我的header.php中,我像这样调用菜单:

<?php wp_nav_menu( array(('container_id' => 'primary_menu_mobile')); ?>

The output is two menus that show up on my phone. 输出是手机上显示的两个菜单。 One is a list. 一个是清单。 all links work as they should. 所有链接都可以正常工作。 The other is drop down with the same menu link. 另一个是使用相同的菜单链接下拉的。 All links here also work as they should. 这里的所有链接也都可以正常工作。 Should i make a seperate menu un wordpress just like my main nav for mobility and call to it instead? 我是否应该像我的主要导航工具那样,在wordpress中制作一个单独的菜单,而不是调用它? I'm confused as to why the menu prints twice. 我对菜单为什么打印两次感到困惑。 I renamed the js file to see what happens without the jquery and the list ul menu still shows up. 我重命名了js文件,以查看在没有jquery的情况下会发生什么,并且仍然显示ul列表菜单。

If I understand your question correctly... 如果我正确理解您的问题...

jQuery("#primary_menu_mobile a").each

The above code seems to imply you know you'll have it as a list of links already, so its just a matter of removing these when you are finished with the each. 上面的代码似乎暗示您知道您已经将其作为链接列表,因此在完成每个链接时只需删除它们即可。

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

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