English 中文(简体)
添加导航替代物
原标题:Adding an Option Sub Menu to Navigation

http://css-tricks.com/convert-menu-to-dropdown” rel=“nofollow”http://csss-tricks.com/convert-menu-to-dropdown。 这对于将标准导航转换成“门努”选择具有很大作用。 我很想知道,如何增加同子公司合作的条件。 举例来说,这里是“WordPress”导航造成......违约。

<ul id= menu-collections >
  <li><a href= http://... >blah</a>
    <ul class= sub-menu >
      <li><a href= http://... >blah</a></li>
    </ul>
  </li>
</ul>

http://jsfiddle.net/eaDLX/"rel=“nofollow” http://jsfiddle.net/eaDLX/

What I would like to do is prepend - to each ul that has a class of sub-menu so that my list looks like this...

选择......

Editorial (this would be the parent)

-- 1920s Collection (this would be the child)

广告

-- Baulmer Collection

......

我确信,这样做非常简单,但我无法作出合乎逻辑的飞跃。 谁能帮助? 成就

        // Create the dropdown base

        $("<select />").appendTo("nav#menu-collections");

        // Create default option "Go to..."
        $("<option />", {
           "selected": "selected",
           "value"   : "",
           "text"    : "Choose a Collection..."
        }).appendTo("nav#menu-collections select");

        // Populate dropdown with menu items
        $("nav#menu-collections a").each(function() {
         var el = $(this);
         $("<option />", {
             "value"   : el.attr("href"),
             "text"    : el.text()
         }).appendTo("nav#menu-collections select");
        });

        $("nav#menu-collections select").change(function() {
         window.location = $(this).find("option:selected").val();
        });

        /* Hide existing menu */
        $( nav#menu-collections ul ).hide();
最佳回答
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            // Create the dropdown base
            $( <select /> ).appendTo( nav#menu-collections );

            // Create default option "Go to..."
            $( <option /> , {
                 selected :  selected ,
                 value :   ,
                 text :  Choose a Collection... ,
                 class :  select 
            }).appendTo( nav#menu-collections select );


            // Populate dropdown with menu items
            $( ul.menu > li ).each(function () {
                var obj = $(this);

                $( <option /> , {
                     value : obj.children( a ).attr( href ),
                     text : obj.children( a ).text()
                }).appendTo( nav#menu-collections select );

                if (obj.children( ul.sub-menu ).length !== 0) {
                    obj.children( ul.sub-menu ).find( a ).each(function () {
                        $( <option /> , {
                             value : $(this).attr( href ),
                             text :  --  + $.trim($(this).text())
                        }).appendTo( nav#menu-collections select );
                    });
                }
            });

            $( nav#menu-collections select ).change(function () {
                if ($(this).find( option:selected ).hasClass( select ) == false) {
                    window.location = $(this).find( option:selected ).val();
                }
            });

            $( nav#menu-collections ul ).remove();
        });
    </script>
</head>
<body>
    <nav id="menu-collections" role="navigation">
    <div class="menu-collections-container">
        <ul id="menu-collections-ul" class="menu">
            <li id="menu-item-53" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-49 current_page_item menu-item-52">
                <a href="#HeaderTest">Header Test</a>
            </li>
            <li id="menu-item-52" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-49 current_page_item menu-item-52">
                <a href="#1920">1920</a>
                <ul class="sub-menu">
                    <li id="menu-item-91" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-91">
                        <a href="#1920Test1">Test1</a> <a href="#1920Test5">Test5</a> <a href="#1920test4">
                            Test4</a> </li>
                </ul>
            </li>
            <li id="menu-item-54" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-49 current_page_item menu-item-52">
                <a href="#HeaderTest2">Header Test 2</a>
            </li>
            <li id="menu-item-55" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-49 current_page_item menu-item-52">
                <a href="#1921">1921</a>
                <ul class="sub-menu">
                    <li id="Li2" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-91">
                        <a href="#1921test">Test1</a> <a href="#1921test3">Test3</a> </li>
                </ul>
            </li>
        </ul>
    </div>
    </nav>
    <h1>
        1920
    </h1>
</body>
</html>
问题回答
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            // Create the dropdown base
            $( <select /> ).appendTo( nav#menu-collections );

            // Create default option "Go to..."
            $( <option /> , {
                 selected :  selected ,
                 value :   ,
                 text :  Choose a Collection... ,
                 class :  select 
            }).appendTo( nav#menu-collections select );


            // Populate dropdown with menu items
            $( ul.menu > li ).each(function () {
                var obj = $(this);
                if (obj.children( ul.sub-menu ).length !== 0) {
                    var subMenuListItems = $( <optgroup /> , {  label : obj.children( a ).text() });

                    if (obj.children( a ).attr( href ) !== undefined) {
                        subMenuListItems.append($( <option /> , {
                             value : obj.children( a ).attr( href ),
                            // text : obj.children( a ).text()   //Or 
                             text :  Home 
                        }));
                    }

                    obj.children( ul.sub-menu ).find( a ).each(function () {
                        subMenuListItems.append($( <option /> , {
                             value : $(this).attr( href ),
                             text : $(this).text()
                        }));
                    });

                    $( nav#menu-collections select ).append(subMenuListItems);
                }
                else {
                    $( <option /> , {
                         value : obj.children( a ).attr( href ),
                         text : obj.children( a ).text()
                    }).appendTo( nav#menu-collections select );
                }
            });

            $( nav#menu-collections select ).change(function () {
                if ($(this).find( option:selected ).hasClass( select ) == false) {
                    window.location = $(this).find( option:selected ).val();
                }
            });

            $( nav#menu-collections ul ).remove();
        });
    </script>
</head>
<body>
    <nav id="menu-collections" role="navigation">
    <div class="menu-collections-container">
        <ul id="menu-collections-ul" class="menu">
            <li id="menu-item-53" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-49 current_page_item menu-item-52">
                <a href="#HeaderTest">Header Test</a>
            </li>
            <li id="menu-item-52" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-49 current_page_item menu-item-52">
                <a href="#1920">1920</a>
                <ul class="sub-menu">
                    <li id="menu-item-91" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-91">
                        <a href="#1920Test1">Test1</a> <a href="#1920Test5">Test5</a> <a href="#1920test4">
                            Test4</a> </li>
                </ul>
            </li>
            <li id="menu-item-54" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-49 current_page_item menu-item-52">
                <a href="#HeaderTest2">Header Test 2</a>
            </li>
            <li id="menu-item-55" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-49 current_page_item menu-item-52">
                <a href="#1921">1921</a>
                <ul class="sub-menu">
                    <li id="Li2" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-91">
                        <a href="#1921test">Test1</a> <a href="#1921test3">Test3</a> </li>
                </ul>
            </li>
        </ul>
    </div>
    </nav>
    <h1>
        1920
    </h1>
</body>
</html>




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签