当前位置: 代码迷 >> JavaScript >> 如何通过id或class将函数限制为仅一个DOM元素
  详细解决方案

如何通过id或class将函数限制为仅一个DOM元素

热度:9   发布时间:2023-06-03 17:51:35.0

我使用这个功能来我的网站上动态地加载网页并能正常工作对我来说只是一个小问题,那就是它遍历所有的锚标记a我的网站上,但我希望它只是重复我的导航菜单! 就这样。 这是我的导航菜单。

<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>

这就是我正在谈论的功能。

     $(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>

正如您在第18和25行看到的那样,他解决了html元素,我尝试使用#nav anav ul#menu a类的不同选择器对其进行更改,但没有任何效果。 您的帮助将不胜感激。

您是否尝试过选择器#menu a 这应该查询<ul id='menu'>元素内的a元素。

  相关解决方案