简体   繁体   中英

how to limit a function to only one DOM element by id or class

i am using this function to load pages dynamically on my site and it works fine for me except one little issue that is it Iterate over all anchor tags a on my site but i want it to iterate only my navigation menu! thats all. here is my naigation menu.

<div id="vertical-menu">

        <nav>
            <ul id='menu' class="menu-items">

                <li><a href="#Browse/Browse_Page1.php"><i class="arcd-archive"></i></br>Browse</a></li>
                <li><a href="#Top_albums/Top_albums_Page0.php"><i class="arcd-music97"></i></br>Top albums</a></li>
                <li><a href="#Top_artists/Top_artists_Page0.php" ><i class="arcd-microphone52"></i></br>Top artists</a></li>
                <li><a href="#Top_lists/Top_lists_Page0.php" ><i class="arcd-numbered8"></i></br>Top lists</a></li>
                <li><a href="#Charts/Charts_Page0.php" ><i class="arcd-rising9"></i></br>Charts</a></li>

            </ul>
        </nav>
</div>

and this is the function i am talking about.

     $(function(){ 
     // Keep a mapping of url-to-container for caching purposes.
     var cache = {
     // If url is '' (no fragment), display this div's content.
     '': $('#default-homepage-contents').fadeIn('fast'),
     };

     // Bind an event to window.onhashchange that, when the history state changes,
     // gets the url from the hash and displays either our cached content or fetches
    // new content to be displayed.
    $(window).bind( 'hashchange', function(e) {

    // Get the hash (fragment) as a string, with any leading # removed. Note that
    // in jQuery 1.4, you should use e.fragment instead of      $.param.fragment().
    var url = $.param.fragment();

    // Remove .bbq-current class from any previously "current" link(s).
    $( 'a.bbq-current' ).removeClass( 'bbq-current' );

   // Hide any visible ajax content.

   $( '#main-container' ).children( ':visible' ).hide();

   // Add .bbq-current class to "current" nav link(s), only if url isn't empty.
   url && $( 'a[href="#' + url + '"]' ).addClass( 'bbq-current' );

   if ( cache[ url ] ) {
  // Since the element is already in the cache, it doesn't need to be
  // created, so instead of creating it again, let's just show it!
  cache[ url ].fadeIn(1000);

   } else {
  // Show "loading" content while AJAX content loads.
  $( '#loading' ).show();

  // Create container for this url's content and store a reference to it in
  // the cache.
  cache[ url ] = $( '<div class="bbq-item"/>' )

    // Append the content container to the parent container.
    .appendTo( '#main-container' )

    // Load external content via AJAX. Note that in order to keep this
    // example streamlined, only the content in .infobox is shown. You'll
    // want to change this based on your needs.
    .load( url, function(){
      // Content loaded, hide "loading" content.
      $( '#loading' ).fadeOut(1000);
    });
 }
})

 // Since the event is only triggered when the hash changes, we need to trigger
// the event now, to handle the hash the page may have loaded with.
$(window).trigger( 'hashchange' );

});
</script>

as you can see on line 18 and 25 he addresses the html element i tried to change it with different selecters like #nav a and nav ul#menu a but nothing working. your help will be appreciated.

Have you tried the selector #menu a ? This should query the a elements within the <ul id='menu'> element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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