首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将类添加到动态加载菜单中

将类添加到动态加载菜单中
EN

Stack Overflow用户
提问于 2013-10-11 16:25:42
回答 2查看 191关注 0票数 0

我的菜单是通过使用web服务返回的javascript来构建的,如下所示:

代码语言:javascript
复制
strGlobalNav='<ul><li><a href="//testonline/" alt="test Online Main Page">testOnline</a></li><li><a href="//testonline/ec/" alt="Employee Center Site">Employee Center</a><ul><li><a href="//testonline/ec" alt="Employee Center Site">Employee Center</a></li><li><a href="//testonline/ec/awards" alt="Awards &amp; Recognition Site">Awards &amp; Recognition</a></li><li><a href="//testonline/hr/benefits" alt="Benefits Site">Benefits</a></li><li><a href="//testonline/hr/EERelations/Pages/Default.aspx" alt="Employee Relations">Employee Relations</a></li><li><a href="//testonline/hr/Employment/Pages/Default.aspx" alt="Employment">Employment</a></li><li><a href="//testonline/ec/training" alt="test University Site">test University</a></li><li><a href="//testonline/ec/healthandsafety" alt="Health &amp; Safety Site">Health &amp; Safety</a></li><li><a href="//testonline/sites/offices" alt="Office Locations Site">Office Locations</a></li><li><a href="//testonline/ec/travel" alt="Travel Site">Travel</a></li><li><a href="//testonline/ec/tso" alt="Total Service Organization">TSO</a></li><li><a href="//testonline/ec/vidconf" alt="Video Conferencing Services">Video Conferencing</a></li></ul></li></ul>'; document.getElementById('test-nav-wrapper').innerHTML = strGlobalNav;

我需要将类添加到包含<ul><li>的父列表项的子菜项中。

这是我的导航。

代码语言:javascript
复制
<div class="globalNav">
  <div id="test-nav-wrapper"></div>
</div>

下面是实际的小提琴:http://jsbin.com/urIlAlO/1/edit

为什么我的脚本不能将类添加到菜单项中。我正在尝试将下拉菜单转换为两个列式超级菜单样式的东西。

请帮助我理解为什么向动态内容添加类不能工作,但是,如果我直接添加html,它会工作吗?这里的例子:http://jsbin.com/iHaqihI/1/edit

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-11 16:42:54

只是一个装载顺序的问题。您可以在DOM准备好时触发jQuery,但可能在添加nav之前,因为它是由javascript添加的。如果在添加类之前将添加的内容移到document函数中,它应该可以工作。看这把更新的小提琴。

http://jsbin.com/urIlAlO/8/

我也把所有的逻辑从头部部分移到身体上,所以它是非阻塞的。实际上,我所做的是将您的文档就绪功能更改为以下内容:

代码语言:javascript
复制
 $(function() {  
           var strGlobalNav='<ul><li><a href="//testonline/" alt="test Online Main Page">testOnline</a></li><li><a href="//testonline/ec/" alt="Employee Center Site">Employee Center</a><ul><li><a href="//testonline/ec" alt="Employee Center Site">Employee Center</a></li><li><a href="//testonline/ec/awards" alt="Awards &amp; Recognition Site">Awards &amp; Recognition</a></li><li><a href="//testonline/hr/benefits" alt="Benefits Site">Benefits</a></li><li><a href="//testonline/hr/EERelations/Pages/Default.aspx" alt="Employee Relations">Employee Relations</a></li><li><a href="//testonline/hr/Employment/Pages/Default.aspx" alt="Employment">Employment</a></li><li><a href="//testonline/ec/training" alt="test University Site">test University</a></li><li><a href="//testonline/ec/healthandsafety" alt="Health &amp; Safety Site">Health &amp; Safety</a></li><li><a href="//testonline/sites/offices" alt="Office Locations Site">Office Locations</a></li><li><a href="//testonline/ec/travel" alt="Travel Site">Travel</a></li><li><a href="//testonline/ec/tso" alt="Total Service Organization">TSO</a></li><li><a href="//testonline/ec/vidconf" alt="Video Conferencing Services">Video Conferencing</a></li></ul></li></ul>';
             $('#test-nav-wrapper').html(strGlobalNav);
            //clean up the row of the mega menu. add css class to each element on bottom row.
            //only if more than 7 elements. if more than 16, mm-3
            jQuery('#test-nav-wrapper ul li ul').each(function(ulindex, ulele){
                $total = jQuery(this).children('li').size();
                if ($total <= 7) {
                    jQuery(this).addClass('mm-1');
                }
                else {
                    $cols = Math.floor(($total) / 8) + 1;
                    $remainder = $total % $cols;
                    $rows = Math.ceil($total / $cols);
                    jQuery(this).addClass('mm-' + $cols + ' total-' + $total + ' rem-'+$remainder );

                    jQuery(this).children().each(function(liindex, liele){
                     //alert("total: "+$total+", remainder: "+ $mod+", ulindex: "+ulindex+", liindex: "+liindex);

                        jQuery(this).addClass('col-' + Math.floor((liindex / $total * $cols)+1) );
                        if( (liindex+1) % $rows == 0) {
                            jQuery(this).addClass('last');
                        }
                    });

                    for (var colcount = 1; colcount<= $cols; colcount++){
                        jQuery(this).children('.col-'+colcount).wrapAll('<div class="col" />');
                    }
                }
            });          


});

更新:只要重读你的问题,注意到‘我的菜单是通过使用返回的javascrip的web服务来构建的’。

在由web服务生成的JS之后,在主体中执行jquery内容。在进行检查和添加类之前,按照下面的步骤将菜单添加到nav,从而启动on ready函数。

代码语言:javascript
复制
$(function() {
  $('#test-nav-wrapper').html(strGlobalNav);
  //clas checking stuff here
})

对你来说应该挺好的。很高兴知道你是如何从web服务中获得菜单的。如果它是同一个域中的服务,请考虑使用$.ajax(),然后执行检查,并在成功函数中添加类。请参阅http://api.jquery.com/jQuery.ajax/

票数 1
EN

Stack Overflow用户

发布于 2013-10-11 16:34:36

在您的原始代码中,您忽略了jQuery(document).ready(function() 中的"{“标记--这就是为什么它不能在那里工作的原因!

代码语言:javascript
复制
       jQuery(document).ready(function()   
       {
        //clean up the row of the mega menu. add css class to each element on bottom row.
        //only if more than 7 elements. if more than 16, mm-3
        jQuery('#test-nav-wrapper ul li ul').each(function(ulindex, ulele){
            $total = jQuery(this).children('li').size();
            if ($total <= 7) {
                jQuery(this).addClass('mm-1');
            }
            else {
                $cols = Math.floor(($total) / 8) + 1;
                $remainder = $total % $cols;
                $rows = Math.ceil($total / $cols);
                jQuery(this).addClass('mm-' + $cols + ' total-' + $total + ' rem-'+$remainder );

                jQuery(this).children().each(function(liindex, liele){
                 //alert("total: "+$total+", remainder: "+ $mod+", ulindex: "+ulindex+", liindex: "+liindex);

                    jQuery(this).addClass('col-' + Math.floor((liindex / $total * $cols)+1) );
                    if( (liindex+1) % $rows == 0) {
                        jQuery(this).addClass('last');
                    }
                });

                for (var colcount = 1; colcount<= $cols; colcount++){
                    jQuery(this).children('.col-'+colcount).wrapAll('<div class="col" />');
                }
            }
        });          

});

小提琴

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19322907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档